stream流处理List对象集合去重
public class TestStream {
private static List
<Student
> list
= new ArrayList<>();
static {
Student s1
= new Student("1", "老大");
Student s2
= new Student("1", "老二");
Student s3
= new Student("2", "老三");
list
.add(s1
);
list
.add(s2
);
list
.add(s3
);
}
public static void main(String
[] args
) {
ArrayList
<Student
> collect1
= list
.stream().collect(Collectors
.collectingAndThen(Collectors
.toCollection(() -> new TreeSet<>(Comparator
.comparing(Student
::getId
))), ArrayList
::new));
collect1
.forEach(x
-> System
.out
.println(x
.getId()+"-"+x
.getName()));
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-27423.html