简体   繁体   中英

Java collection aggregate using lambdas

I am new to java 8 lamdas. I need help on the below collection aggregation and sorted list. I have Arraylist of object which has List

[
[parentName1,parentkey1, child1Name1, childKey1],
[parentName1,parentkey1, child1Name2, childKey2],
[parentName3,parentkey3, child1Name3, childKey3],
[parentName3,parentkey3, child1Name4, childKey4],
[parentName5,parentkey5, child1Name5, childKey5],
[parentName5,parentkey5, child1Name6, childKey6]
]

I would like aggregate above collections using java 8 lambdas in to sorted list.

List(Parent(parentName1,parentkey1,  List(  Child(child1Name1,childKey1),Child(child1Name2, childKey2)  ),
     Parent(parentName3,parentkey3,  List(  Child(child1Name3,childKey3),Child(child1Name4, childKey4)  ),
     Parent(parentName5,parentkey5,  List(  Child(child1Name5,childKey5),Child(child1Name5, childKey6)  )
     );

Any help would appreciated.

I did not understand sorting criteria using which you wanted to sort the list but here is generic example but with little modification it ll work for your case also.

  employeeList.stream()
            .sorted((e1, e2) -> Integer.compare(e1.getEmployeeNumber(), e2.getEmployeeNumber()))
            .forEach(e -> System.out.println(e));

Above example shows examplyee object can be sorted based employee number, but you can change the logic to fit your case.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM