【发布时间】:2019-06-15 07:39:06
【问题描述】:
我有课
public class Person
{
private int _id;
private String _name;
private int _age;
private List<String> _interests;
public Person()
{
}
public Person(int id, String name, int age, List<String> interests)
{
_id = id;
_name = name;
_age = age;
_interests = interests;
}
public int getId()
{
return _id;
}
public void setId(int id)
{
_id = id;
}
public String getName()
{
return _name;
}
public void setName(String name)
{
_name = name;
}
public int getAge()
{
return _age;
}
public void setAge(int age)
{
_age = age;
}
public List<String> getInterests()
{
return _interests;
}
public void setInterests(List<String> interests)
{
_interests = interests;
}
}
我有主方法
public class Main
{
public static void main(String[] args)
{
List<Person> people = Arrays.asList(
new Person(1, "Alex", 23, Arrays.asList("hockey", "football", "google")),
new Person(2, "Brad", 18, Arrays.asList("hockey", "tennis")),
new Person(3, "Felix", 22, Arrays.asList("volleyball", "tennis", "hockey")),
new Person(4, "Brandon", 19, Arrays.asList("hockey", "football", "google"))
);
}
}
问题是,如何使用 stream 和 flatMap 打印年龄超过 21 年的人员及其兴趣而不重复。所以,总的来说,我应该得到“Alex”与曲棍球、足球和 google 以及“Felix”与排球和网球(曲棍球是重复的)
【问题讨论】:
-
"hokey"(重复)将拥有 Alex 而不是 Felix 的决定背后的逻辑是什么? -
预期的输出结构是什么样的?
标签: filter java-8 set java-stream flatmap