You can use groupingBy
collector along with the flatMap
operator to get this done. Here's how it looks.
Map<SomeObject, List<Wrapper>> map = l.stream().flatMap(List::stream)
.collect(Collectors.groupingBy(Wrapper::getSomeObject));
However, an iterative version would be much readable and faster than streams.
Map<SomeObject, List<Wrapper>> m = new HashMap<>();
for (List<Wrapper> wrappers : l)
for (Wrapper w : wrappers)
m.computeIfAbsent(w.someObject, unused -> new ArrayList<Wrapper>()).add(w);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…