• java-lamdba认知


    1.实现Runnable

    new Thread(() -> System.out.println(11)).start();
    

    2.匿名内部类处理方式

    public static void main(String[] args) {
       ww(() -> System.out.println("111"));
    }
    interface MM{
       void aa();
    }
    public static void ww(MM mm){
       mm.aa();
    }
    

    3.对列表操作(循环列表)

    list.forEach(e -> System.out.println(e));
    

    4.函数式编程(Predicate),and、or条件过滤

    List<Integer> list = Arrays.asList(100, 200, 300, 400, 500);
    Predicate<Integer> predicate1 = n -> n >= 100;
    Predicate<Integer> predicate2 = n -> n < 300;
    //与 或  .and  .or
    list.stream().filter(predicate1.and(predicate2)).forEach(e-> System.out.println(e));

    结果:
    100
    200

    5.使用map,进行数据改变

    List<Integer> list = Arrays.asList(100, 200, 300, 400, 500);
    list.stream().map(cost -> cost + 10*cost).forEach(e-> System.out.println(e));

    结果:
    1100
    2200
    3300
    4400
    5500

    注意:原来的list的值并没有改变

    6.流API的 reduce() 方法将所有数字合成一个

    List<Integer> costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
    Integer result = costBeforeTax.stream().map(cost -> cost + 10*cost).reduce((sum,cost)-> sum + cost).get();
    System.out.println(result);

    结果:
    16500

    7.查询满足条件的list

    List<Integer> costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
    costBeforeTax = costBeforeTax.stream().filter(x-> x==100).collect(Collectors.toList());
    System.out.println(costBeforeTax);

    结果:
    100

    8.使用map,进行数据改变,并对数据拼接

    List<String> list = Arrays.asList("a", "b", "c", "d");
    String result = list.stream().map(e-> e.toUpperCase()).collect(Collectors.joining(","));
    System.out.println(result);

    结果:A,B,C,D

    9.数据去重

       a.单个字段

    List<Integer> numbers = Arrays.asList(9, 10, 3, 4, 9, 3, 4);
    List<Integer> distinct = numbers.stream().distinct().collect(Collectors.toList());
    System.out.println(numbers);
    System.out.println(distinct);

    结果:
    [9, 10, 3, 4, 9, 3, 4]
    [9, 10, 3, 4]

        b.对象

    BookBo bookBo1 = new BookBo("1", "语文");
    BookBo bookBo2 = new BookBo("2", "数学");
    BookBo bookBo3 = new BookBo("3", "英语");
    BookBo bookBo4 = new BookBo("4", "地理");
    BookBo bookBo5 = new BookBo("5", "生物");
    BookBo bookBo6 = new BookBo("1", "语文");
    List<BookBo> bookBoList = Arrays.asList(bookBo1, bookBo2, bookBo3, bookBo4, bookBo5, bookBo6);
    
    List<BookBo> distinctList = bookBoList
                    .stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(o-> o.getName()))), ArrayList::new))
                    .stream().sorted(Comparator.comparing(BookBo::getName)).collect(Collectors.toList());
    
    System.out.println(distinctList);

    结果:
    [ BookBo{name='1', author='语文'},

      BookBo{name='2', author='数学'},

      BookBo{name='3', author='英语'},

      BookBo{name='4', author='地理'},

      BookBo{name='5', author='生物'}]

    10.list转map

    BookBo bookBo1 = new BookBo("1", "语文","a");
    BookBo bookBo2 = new BookBo("2", "数学","b");
    BookBo bookBo3 = new BookBo(null, "英语","c");
    BookBo bookBo4 = new BookBo("4", "地理","d");
    BookBo bookBo5 = new BookBo("5", "生物","e");
    BookBo bookBo6 = new BookBo("6", "生物","f");
    BookBo bookBo7 = new BookBo("7", "","g");
    List<BookBo> bookBoList = Arrays.asList(bookBo1, bookBo2, bookBo3, bookBo4, bookBo5, bookBo6, bookBo7);
     //(k1,k2)-> k2  如果key值重复,取第二个
    Map<String,BookBo> map = bookBoList.stream()
                    .filter(x-> !StringUtils.isEmpty(x.getName()) && !StringUtils.isEmpty(x.getAuthor()))
                    .collect(Collectors.toMap(k->k.getName()+"_"+k.getAuthor(), bookBo -> bookBo,(k1, k2)-> k2));
    map.forEach((k,v)-> System.out.println(k+","+v));

    结果:
    1_语文,BookBo{name='1', author='语文', value='a'}
    4_地理,BookBo{name='4', author='地理', value='d'}
    2_数学,BookBo{name='2', author='数学', value='b'}
    6_生物,BookBo{name='6', author='生物', value='f'}
    5_生物,BookBo{name='5', author='生物', value='e'}

    11.最大值、最小值、总和、平均值

    List<Integer> primes = Arrays.asList(2, 3, null, 7, 11, 13, 17, 19, 23, 29);
    IntSummaryStatistics stats = primes.stream().filter(e-> e!=null).mapToInt((x) -> x).summaryStatistics();
    System.out.println(stats.getMax());
    System.out.println(stats.getMin());
    System.out.println(stats.getSum());
    System.out.println(stats.getAverage());

    结果:
    29
    2
    124
    13.777777777777779

    12.排序

        a.单个字段排序

    String[] atp = {"2016-06-28 08:00:00", "2017-12-05 19:17:32","2017-03-06 22:14:51","2016-12-28 08:00:00", "2017-03-05 19:17:32","2017-03-06 22:14:51"};
    Arrays.sort(atp, Comparator.reverseOrder());
    Arrays.asList(atp).forEach(System.out::println);
    System.out.println("---");
    Arrays.sort(atp, Comparator.naturalOrder());
    Arrays.asList(atp).forEach(System.out::println);

    结果:

    2017-12-05 19:17:32
    2017-03-06 22:14:51
    2017-03-06 22:14:51
    2017-03-05 19:17:32
    2016-12-28 08:00:00
    2016-06-28 08:00:00
    ---

    2016-06-28 08:00:00
    2016-12-28 08:00:00
    2017-03-05 19:17:32
    2017-03-06 22:14:51
    2017-03-06 22:14:51
    2017-12-05 19:17:32

        b.对象

    BookBo bookBo1 = new BookBo("2", "语文","a");
    BookBo bookBo2 = new BookBo(null, "数学","b");
    BookBo bookBo3 = new BookBo("3", "英语","c");
    BookBo bookBo4 = new BookBo("4", "地理","d");
    BookBo bookBo5 = new BookBo("5", "生物","e");
    BookBo bookBo6 = new BookBo("7", "生物","f");
    BookBo bookBo7 = new BookBo("6", "","g");
    List<BookBo> bookBoList = Arrays.asList(bookBo1, bookBo2, bookBo3, bookBo4, bookBo5, bookBo6, bookBo7);
    //如果数据中有null,进行空处理 Comparator.nullsFirst(String::compareTo)
    Collections.sort(bookBoList, Comparator.comparing(BookBo::getName, Comparator.nullsFirst(String::compareTo)));
    bookBoList.forEach(e-> System.out.println(e));
    System.out.println("--");
    Collections.sort(bookBoList, Comparator.comparing(BookBo::getName, Comparator.nullsFirst(String::compareTo)).reversed());
    bookBoList.forEach(e-> System.out.println(e));

    结果:

    BookBo{name='null', author='数学', value='b'}
    BookBo{name='2', author='语文', value='a'}
    BookBo{name='3', author='英语', value='c'}
    BookBo{name='4', author='地理', value='d'}
    BookBo{name='5', author='生物', value='e'}
    BookBo{name='6', author='', value='g'}
    BookBo{name='7', author='生物', value='f'}
    --
    BookBo{name='7', author='生物', value='f'}
    BookBo{name='6', author='', value='g'}
    BookBo{name='5', author='生物', value='e'}
    BookBo{name='4', author='地理', value='d'}
    BookBo{name='3', author='英语', value='c'}
    BookBo{name='2', author='语文', value='a'}
    BookBo{name='null', author='数学', value='b'}

     13.查询列表中,code=1的数据,返回第一个

            DeptKind deptKind = DeptKind.CFBDW;
            List<Dept> listDept = new ArrayList<>();
            Dept de = new Dept();
            de.setDeptKindId(2l);
            listDept.add(de);
            Optional<Dept> op = listDept.stream().filter(p -> deptKind.getCode().equals(p.getDeptKindId())).findFirst();
            //是否有值   这个一定要判断,如果不判断,数据不存在的情况下,会报错:No value present
            if(op.isPresent()){
                System.out.println(op.get().getDeptKindId());
            }

    14.对列表的元素数据进行修改

    ww.forEach(vo -> {vo.setEndDate(new Date());vo.setId(11l);});

    15.返回列表中某个键的列表

    List<Integer> list1 = list.stream().map(A::getId).collect(Collectors.toList());
    List<Integer> list2 = list.stream().map(A::getAge).collect(Collectors.toList());//未去重
    List<Integer> list3 = list.stream().map(A::getAge).distinct().collect(Collectors.toList());//已去重

    16. list中,进行group by,得到结果

    Map<String, Integer> collectTemp = list.stream().collect(Collectors.groupingBy(ForbidTypeAnalysisDto::getForbidTypeName, Collectors.summingInt(ForbidTypeAnalysisDto::getCount)));
    
    collectTemp.forEach((key,value) -> System.out.println(key + "," + value));
  • 相关阅读:
    PyCharm设置改变字体大小的快捷键
    python中的字符串
    python入门知识
    css3(border-radius)边框圆角详解
    js中__proto__和prototype的区别和关系?
    常见的浏览器兼容问题
    Meta http-equiv属性详解(转)
    WinForm界面设计-Button添加背景图去边框
    vs2015 c# winfrom应用程序打包成64位
    vsto-Word相关操作
  • 原文地址:https://www.cnblogs.com/qjm201000/p/12259083.html
Copyright © 2020-2023  润新知