java8剖析

java新特新的介绍

直接上代码吧

list的遍历
		studentList.forEach(i -> { System.out.println(i) ;            } );
filter的用法
// 1 查询出学生名字为张三的学生.
List<Student> list = studentList.stream().filter(a -> a.getName().equals("张三")).collect(Collectors.toList());
  // 2 查询出成绩大于80分的学生.
  List<Student> collect = studentList.stream().filter(a -> a.getGrade() > 80).collect(Collectors.toList()) ;
  // 3 查询出不是男的学生性别
  List<Student> list2 = studentList.stream().filter(a -> a.getSex() != "男").collect(Collectors.toList());
  // 按照学生成绩排序,从小到大顺序.
   studentList.sort((student1,student2) -> student1.getGrade().compareTo(student2.getGrade()));
list转化成map的用法
把list转化成map.注意第一个当key出现相同时,会报错.
 Map<String, Student> collect = studentList.stream().collect(Collectors.toMap((key->key.getId()),(value->value) ));
 Map<String, Student> collect1 = studentList.stream().collect(Collectors.toMap(Student::getId, (m) -> m, (existsValue, newValue) -> newValue ));
list转化成list的用法
 //获取list对象的某个字段组装成新list
 List<String> userIdList = studentList.stream().map(a -> a.getId()).collect(Collectors.toList());

Lambda的求和,平均数,最值…
 int sumAge = studentList.stream().mapToInt(Student :: getGrade).sum() ;
 System.out.println(sumAge);
 OptionalDouble average = studentList.stream().mapToInt(Student :: getGrade).average();
 System.out.println(average);
 OptionalInt max = studentList.stream().mapToInt(Student :: getGrade).min();
 Integer integer = studentList.stream().map(Student :: getGrade).min(Integer::compareTo).get();
 System.out.println(max.getAsInt());
 System.out.println(integer);
求和的另一种形式
自定义BigDecimalUtils类,解决null的问题
BigDecimal totalQuantity = userList.stream().map(User::getFamilyMemberQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal totalQuantity2 = userList.stream().map(User::getFamilyMemberQuantity).reduce(BigDecimal.ZERO, BigDecimalUtils::sum);  

 public static BigDecimal ifNullSet0(BigDecimal in) {
       if (in != null) {
           return in;
       }
       return BigDecimal.ZERO;
  }

 public static BigDecimal sum(BigDecimal ...in){
       BigDecimal result = BigDecimal.ZERO;
       for (int i = 0; i < in.length; i++){
           result = result.add(ifNullSet0(in[i]));
       }
       return result;
  }
list的分组
Map<String, List<Student>> groupBySex = studentList.stream().collect(Collectors.groupingBy(Student::getSex));
        System.out.println(groupBySex);
        //遍历分组
        for (Map.Entry<String, List<Student>> entryUser : groupBySex.entrySet()) {
            String key = entryUser.getKey();
            List<Student> entryUserList = entryUser.getValue();
            System.out.println(key);
            System.out.println(entryUserList);
        }

排序

ArrayList<User> collect = list.stream().filter(a -> !StringUtils.isEmpty(a.getUsername()))
		.collect(Collectors.collectingAndThen(Collectors.toCollection(()	 -> new TreeSet<>(Comparator.comparing(User::getId))
                ), ArrayList::new));
List.stream().sorted(Comparator.comparing(Entity::getAmount).reversed().thenComparing(Entity::getId)).collect(Collectors.toList());

未完…

作者 : 连理枝
来源:CSDN
转载请注明出处 : https://blog.csdn.net/qq_41513129/article/details/88972453
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值