【发布时间】:2020-08-24 23:41:59
【问题描述】:
我有一个HashMap,其键类型为HashSet,值类型为ArrayList。
将所有键值对插入到Map 后。我想迭代每个键及其值以进行进一步处理,而不仅仅是打印,它将用于键和值作为字符串参数传递的进一步过程。
所以我在想这是否可能,而不是在 Map 中一次迭代每个键值对,即等待第一次迭代结束以完成整个过程,然后才开始下一次迭代。相反,如果可以并行迭代所有键值而不必等待轮到它,那么整体结果会更快。迭代的顺序并不重要。它应该只并行读取所有键值对。
我尝试了 Stream API,但这只是并行打印所有迭代值,但在进一步的过程中,它并没有达到我的预期,也许我做错了什么,因为我不熟悉 Streams。如果使用可用的内置函数或类无法实现这一点,那么即使是第三方 Jars(Apache-commons)也可以。
这是我迄今为止尝试过的代码:
String jobId = "J1";
Map<Set<String>,List<String>> map_batch_result_details = new HashMap<Set<String>,List<String>>();
Set<String> hs_batchesId= new HashSet<String>();
List<String> list_resultId = new ArrayList<String>();
hs_batchesId.add("B1");
hs_batchesId.add("B2");
hs_batchesId.add("B3");
list_resultId.add("R1");
list_resultId.add("R2");
list_resultId.add("R3");
map_batch_result_details.put(hs_batchesId, list_resultId);
map_batch_result_details.entrySet().stream().forEach(e -> {
System.out.format("key: %s, value: %s%n", e.getKey(), e.getValue()); // for printing results using stream without iterating each row sequentially instead iterates it parallely
InputStream inputStream = Connection.getQueryResultStream(jobId, e.getKey().toString(), e.getValue().toString());
/*
getQueryResultStream expects one batchId which is <Key> of Map but it needs it in String so I am using toString
similarly third parameter expects one resultId which is <value> of Map again in String so toString
But Code fails as it is not passing 1 key and 1 value, rather it passes all key values at once in single call.
*/
});
输出
CALL: getQueryResultStream(jobId, e.getKey().toString(),e.getValue().toString());
**Actual values passed**
getQueryResultStream(J1,[B1,B2,B3],[R1,R2,R3])
**Expected values**
getQueryResultStream(J1,B1,R1)
getQueryResultStream(J1,B2,R2)
getQueryResultStream(J1,B3,R3)
Expected value should execute in Parallel without having to wait for first iteration to get over. But not in one call which is happening in Actual values.
【问题讨论】:
-
你试过
map_batch_result_details.entrySet().parallelStream()而不是map_batch_result_details.entrySet().stream()吗? -
@mexicomanni 即使使用 parallelStream(),它仍然在方法 getQueryResultStream(jobId, e.getKey().toString(), e.getValue().toString() 中传递 Map 的所有键和值与预期结果匹配。请查看我的帖子,我添加了实际和预期结果。
-
你的HashMap中只有一个键值对,所以它不能并行处理任何东西。
-
@mexicomanni 我的错。感谢您的指出。
标签: java java-8 parallel-processing hashmap java-stream