我是 java 8 lamda 的新手。我需要有关以下集合聚合和排序列表的帮助。 我有对象的数组列表,其中有列表
[
[parentName1,parentkey1, child1Name1, childKey1],
[parentName1,parentkey1, child1Name2, childKey2],
[parentName3,parentkey3, child1Name3, childKey3],
[parentName3,parentkey3, child1Name4, childKey4],
[parentName5,parentkey5, child1Name5, childKey5],
[parentName5,parentkey5, child1Name6, childKey6]
]
我想使用 java 8 lambda 将以上集合聚合到排序列表中。
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) )
);
如有任何帮助,我们将不胜感激。
最佳答案
我不明白您想要用来对列表进行排序的排序标准,但这里是通用示例,但只需稍作修改,它也适用于您的情况。
employeeList.stream()
.sorted((e1, e2) -> Integer.compare(e1.getEmployeeNumber(), e2.getEmployeeNumber()))
.forEach(e -> System.out.println(e));
上面的示例显示 exampleplyee 对象可以根据员工编号进行排序,但您可以更改逻辑以适合您的情况。
关于使用 lambda 进行 Java 集合聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35925406/