List.stream 获取某个字段的数据去重
创建实体
public class person {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
过滤数据重复字段
public static void main(String[] args) {
List<person> personList = new ArrayList<>();
personList.add(new person(1,"李小明",12));
personList.add(new person(2,"王红",12));
personList.add(new person(3,"张强",13));
personList.add(new person(4,"李明",15));
Set<Integer> collect = personList.stream().map(person::getAge).collect(Collectors.toSet());
System.out.printf(collect.toString());
}