The collect method of the Stream class

The collect method of the Stream class

Reference:  https://www.jianshu.com/p/ccbb42ad9551

 

  • Collectors.toSet(): Convert to set collection.
  • Collectors.toCollection(TreeSet::new): Convert to a specific set collection.
    TreeSet<Integer> collect2 = Stream.of(1, 3, 4).collect(Collectors.toCollection(TreeSet::new));
  • Collectors.toMap(x -> x, x -> x + 1):转换成map。
    Map<Integer, Integer> collect1 = Stream.of(1, 3, 4).collect(Collectors.toMap(x -> x, x -> x + 1));
  • Collectors.minBy(Integer::compare): To find the minimum value, of course, there is also a maxBy method.
  • Collectors.averagingInt(x->x): Average value, and there are also averagingDouble and averagingLong methods.
    System.out.println(Stream.of(1, 2, 3).collect(Collectors.averagingInt(x->x)));
  • Collectors.summingInt(x -> x)): Sum.
  • Collectors.summarizingDouble(x -> x): You can get the maximum value, minimum value, average value, sum value, and total number.
    DoubleSummaryStatistics summaryStatistics = Stream.of(1, 3, 4).collect(Collectors.summarizingDouble(x -> x));
    summaryStatistics.getAverage(); //average
  • Collectors.groupingBy(x -> x) has three methods. Looking at the source code, you can see that the first two methods eventually call the third method. The second parameter defaults to HashMap::new The third parameter defaults to Collectors.toList(), refer to SQL groupBy.
    Map<Integer, List<Integer>> map = Stream.of(1, 3, 3, 4).collect(Collectors.groupingBy(x -> x));
    Map<Integer, Long> map = Stream.of(1 , 3, 3, 4).collect(Collectors.groupingBy(x -> x, Collectors.counting()));
    HashMap<Integer, Long> hashMap = Stream.of(1, 3, 3, 4).collect( Collectors.groupingBy(x -> x, HashMap::new, Collectors.counting()));
  • Collectors.partitioningBy(x -> x > 2), divide the data into two parts, the key is true/false. The first method also calls the second method, and the second parameter defaults to Collectors.toList().
    Map<Boolean, List<Integer>> collect5 = Stream.of(1, 3, 4).collect(Collectors.partitioningBy(x -> x > 2));
    Map<Boolean, Long> collect4 = Stream.of(1 , 3, 4).collect(Collectors.partitioningBy(x -> x > 2, Collectors.counting()));
  • Collectors.joining(","):拼接字符串。
    System.out.println(Stream.of("a", "b", "c").collect(Collectors.joining(",")));
  • Collectors.reducing(0, x -> x + 1, (x, y) -> x + y)): When calculating the accumulated value, you can also change the parameter value, here is +1 and then sum . Similar to the reduce method, but the reduce method does not have a second parameter.
    System.out.println(Stream.of(1, 3, 4).collect(Collectors.reducing(0, x -> x + 1, (x, y) -> x + y)));
  • Collectors.collectingAndThen(Collectors.joining(","), x -> x + "d"): Execute the collect operation first and then execute the expression of the second parameter. Here, the string is concatenated first, then + "d" at the end.
    String str= Stream.of("a", "b", "c").collect(Collectors.collectingAndThen(Collectors.joining(","), x -> x + "d"));
  • Collectors.mapping(...): Similar to the map operation, but the parameters are slightly different.
    System.out.println(Stream.of("a", "b", "c").collect(Collectors.mapping(x -> x.toUpperCase(), Collectors.joining(","))));
Author: Matcha Fungus
Link : https://www.jianshu.com/p/ccbb42ad9551
Source: Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.
 
===================
    public static void test003() throws Exception {
        TreeSet<Integer> collect2 = Stream.of(1, 3, 4, 2).collect(Collectors.toCollection(TreeSet::new));
        System.out.println(collect2);// [1, 2, 3, 4]

        List<KeyValue<Integer, String>> kvs = Lists.newArrayList();
        for (int i = 0; i < 3; i++) {
            kvs.add(new KeyValue<>(i, "hello-" + i));
        }
        Map<Integer, String> map = kvs.stream().collect(Collectors.toMap(KeyValue::getKey, kv -> {
            return kv.getValue() + "&&" + kv.getKey();
        }));
        System.out.println(map);// {0=hello-0&&0, 1=hello-1&&1, 2=hello-2&&2}

        String joinResult = Stream.of("a", "b", "c")
                .collect(Collectors.mapping(x -> x.toUpperCase(), Collectors.joining(",")));
        System.out.println(joinResult);// A,B,C

        Map<Integer, Integer> collect1 = Stream.of(1, 3, 4).collect(Collectors.toMap(x -> x, x -> x + 1));
        System.out.println(collect1);// {1=2, 3=4, 4=5}

        // Collectors.joining(","): join strings.
        System.out.println(Stream.of("a", "b", "c").collect(Collectors.joining(",")));// a,b,c

        System.out.println(Stream.of("a", "b", "c")
                .collect(Collectors.collectingAndThen(Collectors.joining(","), x -> x + "d")));// a,b,cd

    }
  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326236958&siteId=291194637