java8 流操作 List去重

阅读 74

2022-11-07


List<String> temp = sList.stream().map(x -> x.getId()).distinct().collect(Collectors.toList());

流操作,前面是展开 map,对指定id进行去重,最后转为list

其中 List<Stu> sList
Stu为对象,id 和name两个字段


其他操作参见:jdk stream

再有例子:
Optional<Integer> score = studentList.stream()
.map(Student :: getScore)
.filter(s -> s<60)
.reduce((a,b) -> a+b);

精彩评论(0)

0 0 举报