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!
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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.