Java Code Like:
List<Detail> DbDetails = ... Like 50000 rows records
Map<Long, List<Detail>> details = new HashMap();
DbDetails .parallelStream().forEach(detail -> {
Long id = detail.getId();
details.computeIfAbsent(id, v -> new ArrayList<>()).add(detail);
});
Then ...
details.entrySet().stream().forEach(e -> {
e.getValue(); // Some value is empty
});
我猜是因为HashMap是线程不安全的,所以我使用Hashtable而不是它。然后它运行正常,所有值都有价值,但我不知道为什么?
答案 0 :(得分:3)
HashMap
不是线程安全的,因此不要使用并行流。
此外,为什么那样,当流可以为你做到这一点?
DbDetails.parallelStream().collect(Collectors.groupingBy(Detail::getId))