stream()流式编程

1. 判断集合中的某个元素的某个属性是否全部相等
List<User> users = new ArrayLiat<>();
	//获取User对象age属性,distinct()去重,count()获取去重后的集合数,如果全部相等:count应该等于1
    long count = users.stream().map(User::getAge).distinct().count();

2. 根据对象中某个元素进行分组
List<User> users = new ArrayLiat<>();
Map<String,List<User>> map = users .stream().collect(Collectors
                .groupingBy(User::getUserCode));
3.根据集合对象中的某个元素去重
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("1","小c","18"));
studentList.add(new Student("2","小b","18"));
studentList = studentList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student :: getAge))), ArrayList::new));

4.根据集合对象中某个元素排序
//1.倒序
studentList .stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());
//2.正序
 studentList .stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());  
//使用lambda表达式
list = list.stream().sorted(Comparator.comparing(e -> e.getAge())).collect(Collectors.toList());
//多条件-先年龄升序、后分数升序
list = list.stream().sorted(Comparator.comparing(Student::getAge).thenComparing(Comparator.comparing(Student::getScore))).collect(Collectors.toList());

list 去重
list = list.stream().distinct().collect(Collectors.toList())
list转换.
        List<OmpInterfaceVersionResDO> twoLevelSubjects = ListBeanUtils.copyListProperties(lists, OmpInterfaceVersionResDO::new);
     
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值