【发布时间】:2022-01-11 03:52:59
【问题描述】:
我有一份名单: 爱好清单{游戏,滑雪,足球......) 高度 重量 出生年份
我想显示 1999 年出生的 20 个人中最重的 5 个人的爱好列表(不重复)
我有一个流列表:
List<String> question7 = ListPerson.stream()
.filter( p -> p.getDateDeNaissance().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().getYear() == 1999)
.sorted(Comparator.comparingDouble(p -> ((person) p).getHeight()).reversed())
.limit(20)
.sorted(Comparator.comparingDouble(p -> ((Individu) p).getWeight()).reversed())
.limit(5)
.map(s->s.getListHobbys())
.flatMap(Collection::stream)
.collect(Collectors.toList());
但我没有我想要的,而且有冗余。有人可以问我谁能做到这一点
【问题讨论】:
-
我推荐使用
Stream::distinct。 -
我这样解决了
.flatMap(Collection::stream).distinct() -
随便叫吧。这是一个中间流操作。
-
是的,谢谢
标签: java list stream java-stream