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

发布了12 篇原创文章 · 获赞 0 · 访问量 2018

猜你喜欢

转载自blog.csdn.net/LOVELYJF/article/details/103072704