zoukankan      html  css  js  c++  java
  • Java8新特性

    1:将Enum中的值转换为集合

      List<String> enumNames = Stream.of(Enum.values()

      .  .map(Enum::name)

        .collect(Collectors.toList());

    2:集合获取交集,并集,差集,去重并集,List集合中的重复次数

      交集:List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());

      差集:List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());

      并集:List<String> listAll = list1.parallelStream().collect(toList());
            List<String> listAll2 = list2.parallelStream().collect(toList());
            listAll.addAll(listAll2);
      去重并集:List<String> listAllDistinct = listAll.stream().distinct().collect(toList());

      重复次数:Map<String, Long> map = list.stream()
                      .collect(Collectors.groupingBy(p -> p, Collectors.counting()));

           //v:重复的次数 k重复的值
                  map.forEach((k, v) -> System.out.println(k + ":" + v));
          // 老师集合
            List<Teacher> teachers = Arrays.asList(
                    new Teacher(1L, "张三"),
                    new Teacher(2L, "李四"),
                    new Teacher(3L, "王五"),
                    new Teacher(4L, "赵六"));

            // 学生集合
            List<Student> students = Arrays.asList(
                    new Student(5L, "张三"),
                    new Student(6L, "李四"),
                    new Student(7L, "小红"),
                    new Student(8L, "小明"));

            // 求同时出现在老师集合和学生集合中的人数,name相同即视为同一个人
            int size = teachers.stream()
                    .map(t -> students.stream().filter(s -> Objects.nonNull(t.getName()) && Objects.nonNull(s.getName()) && Objects.equals(t.getName(), s.getName())).findAny().orElse(null))
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList())
                    .size();

            // 求同时出现在老师集合和学生集合中人的name集合,name相同即视为同一个人
            List<String> names = teachers.stream()
                    .map(t -> students.stream().filter(s -> Objects.nonNull(t.getName()) && Objects.nonNull(s.getName()) && Objects.equals(t.getName(), s.getName())).findAny().orElse(null))
                    .filter(Objects::nonNull)
                    .map(r -> r.getName())
                    .collect(Collectors.toList());

    3:Map集合排序

       Map<K, V> result = Maps.newLinkedHashMap();

      根据key排序

        降序:map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey().reversed()) .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));

        升序:map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey()) .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));

      根据value排序

        降序:map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByValue().reversed()) .forEach(e -> result.put(e.getKey(), e.getValue()));

        升序:map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByValue()) .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));

    4:List集合sorted排序null问题

      当排序属性值不为null时:

        List<WellReservoirFormationInfo> voList = null;
        voList = voList.stream().sorted(Comparator.comparing(User::getUserName)).collect(Collectors.toList());
      当排序属性值为null时:

        voList = voList.stream().sorted(Comparator.comparing(User::getUserName,Comparator.nullsLast(String::compareTo))) .collect(Collectors.toList());

  • 相关阅读:
    setTimeout中0毫秒延时
    javascript中call和apply方法
    javascript闭包
    apns 服务
    新的开始,新的起点
    心情笔记
    如何解决控件附件上传时超大附件无法上传的问题
    BPM实例分享——日期自动计算
    BPM实例分享——金额规则大写
    分享一个程序猿在流程数据查看权限问题的总结
  • 原文地址:https://www.cnblogs.com/threeboke/p/15386134.html
Copyright © 2011-2022 走看看