600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 使用 Java8的 stream对list数据去重 使用filter()过滤列表 list转map

使用 Java8的 stream对list数据去重 使用filter()过滤列表 list转map

时间:2019-02-20 22:38:21

相关推荐

使用 Java8的 stream对list数据去重 使用filter()过滤列表 list转map

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());

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。