list去重,根据对象某个属性、某几个属性去重
去除List中重复的String
List unique = list.stream().distinct().collect(Collectors.toList());
去除List中重复的对象
// Person 对象public class Person { private String id; private String name; private String sex; <!--省略 get set-->}// 根据name去重List<Person> unique = persons.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(paring(Person::getName))), ArrayList::new));// 根据name,sex两个属性去重List<Person> unique = persons.stream().collect( Collectors. collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(paring(o -> o.getName() + ";" + o.getSex()))), ArrayList::new));filter()过滤列表
List<Person> filterList = persons.stream().filter(p -> p.getSex().equals(1)).collect(Collectors.toList());List转Map
从一个Person对象的List集合,取出id和name组成一个map集合
Map<String, String> collect = list.stream().collect(Collectors.toMap(p -> p.getId(), p -> p.getName()));从 List 中取出某个属性的组成 list 集合
//1.提取出list对象中的一个属性List<String> stIdList1 = stuList.stream().map(Person::getId).collect(Collectors.toList());//2.提取出list对象中的一个属性并去重List<String> stIdList2 = stuList.stream().map(Person::getId).distinct().collect(Collectors.toList());