Lambda(高级用法)

一:lambda的 (对象)排序

   List<Student> list = new ArrayList<>();

        List<Student> resultList = new ArrayList<>();

        try {

                Student sd = Student.class.newInstance();

                sd.setK1("A");
                sd.setK2("E");
                sd.setV3(3);
            list.add(sd);

            Student sd1 = Student.class.newInstance();
            sd1.setK1("B");
            sd1.setK2("F");
            sd1.setV3(4);
            list.add(sd1);


            Student sd2 = Student.class.newInstance();
            sd2.setK1("B");
            sd2.setK2("F");
            sd2.setV3(1);
            list.add(sd2);

            Student sd3 = Student.class.newInstance();
            sd3.setK1("B");
            sd3.setK2("E");
            sd3.setV3(2);

            list.add(sd3);
            //(1) 排序(升序)
            List<Student> soreList = list.stream().sorted(Comparator.comparing(Student::getV3)).collect(Collectors.toList());
//降序
 List<Student> soreList =    list.stream().sorted(Comparator.comparing(Student::getV3).reversed()).collect(Collectors.toList());

//           for(Student s : soreList){
//
//             System.out.println(s.getV3());
//           }   //遍历

            soreList.stream().map(s -> s.getV3()).forEach(s ->{System.out.println(s);}); //遍历
//              soreList.stream().map(student -> student).forEach(s->{
//
//                 if(!resultList.stream().map(student -> student.getK1()).collect(Collectors.toList()).contains(s.getK1())
//                     &&   !resultList.stream().map(student -> student.getK2()).collect(Collectors.toList()).contains(s.getK2())
//
//
//
//                         )
//                     resultList.add(s);
//                 {
//
//
//                 }
//
//
//              });

 

排序 可以参考下面 

https://www.cnblogs.com/heqiyoujing/p/9769916.html

map 映射
中间操作map会将元素根据指定的Function接口来依次将元素转成另外的对象,下面的示例展示了将字符串转换为大写字符串。 你也可以通过map来讲对象转换成其他类型,map返回的Stream类型是根据你map传递进去的函数的返回值决定的。

List<String> list = Arrays.asList("test","hello","world","java","tom","C","javascript");
list.stream().map(s->s.toUpperCase()).forEach(System.out::println);

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值