Reputation: 136
Java 8 Lambda Map for each create a new object and add this new objet in a list
This is my firts step
Map<Integer, List<Obj>> objGroupBy = list.stream().collect(Collectors.groupingBy(Obj::getSomething));
List<Obj2> lst2= new ArrayList<Obj2>();
for (Entry<Integer, List<Obj>> entry : objGroupBy .entrySet())
{
lst2.add(new Obj2(entry.getKey().intValue(), entry.getValue()));
}
I want to know if it possible to do it in lambda Thanks!
Upvotes: 0
Views: 2589
Reputation: 53819
Equivalent to your code:
List<Obj2> lst2 = list.stream()
.map(o -> new Obj2(o.getSomething(), o))
.collect(Collectors.toList());
Now if you really want to start the same way:
List<Obj2> lst2 = list.stream()
.collect(Collectors.groupingBy(Obj::getSomething))
.entrySet()
.stream()
.map(e -> new Obj2(e.getKey(), e.getValue()))
.collect(Collectors.toList());
Upvotes: 2