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

原文地址:https://www.cnblogs.com/threeboke/p/15386134.html