Java Stream groupingBy() 操作

版权声明:望支持~~~ https://blog.csdn.net/u011663149/article/details/87883739

对Stream的List<T>等更多操作进行操可参考https://blog.csdn.net/u011663149/article/details/86743930

前言: 

       groupingBy() 提供与SQL的GROUP BY子句类似的功能,只有Java Stream API才有。为了使用它,我们需要指定一个用于执行分组的属性。我们通过提供功能接口的实现来实现这一点。通常通过传递lambda表达式。

Example1

//根据字符串长度
List<String> strings = List.of("a", "bb", "cc", "ddd"); 
Map<Integer, List<String>> result = strings.stream().collect(groupingBy(String::length)); 
System.out.println(result); // {1=[a], 2=[bb, cc], 3=[ddd]}

Example2

//分组输出指定的Map集合
List<String> strings = List.of("a", "bb", "cc", "ddd");
TreeMap<Integer, List<String>> result = strings.stream().collect(groupingBy(String::length, TreeMap::new, toList()));
System.out.println(result); // {1=[a], 2=[bb, cc], 3=[ddd]}
//同理上面
List<String> strings = List.of("a", "bb", "cc", "ddd");
Map<Integer, TreeSet<String>> result = strings.stream().collect(groupingBy(String::length, toCollection(TreeSet::new)));
System.out.println(result); // {1=[a], 2=[bb, cc], 3=[ddd]}

Example3

// counting() 计数收集器
List<String> strings = List.of("a", "bb", "cc", "ddd");
Map<Integer, Long> result = strings.stream().collect(groupingBy(String::length, counting()));
System.out.println(result); // {1=1, 2=2, 3=1}

Example4

//join连接结果数据
List<String> strings = List.of("a", "bb", "cc", "ddd");
Map<Integer, String> result = strings.stream().collect(groupingBy(String::length, joining(",", "[", "]")));
System.out.println(result); // {1=[a], 2=[bb,cc], 3=[ddd]}

Example5 

//分组过滤操作 filtering
List<String> strings = List.of("a", "bb", "cc", "ddd");
Map<Integer, List<String>> result = strings.stream().collect(groupingBy(String::length, filtering(s -> !s.contains("c"), toList())));
System.out.println(result); // {1=[a], 2=[bb], 3=[ddd]}

Example6

 *averagingInt() 
 *averagingLong() 
 *averagingDouble() 
//分组取 平均数
List<String> strings = List.of("a", "bb", "cc", "ddd");
Map<Integer, Double> result = strings.stream().collect(groupingBy(String::length, averagingInt(String::hashCode)));
System.out.println(result); // {1=97.0, 2=3152.0, 3=99300.0}

 *summingInt() 
 *summingLong() 
 *summingDouble()
//分组求和
List<String> strings = List.of("a", "bb", "cc", "ddd");
Map<Integer, Integer> result = strings.stream().collect(groupingBy(String::length, summingInt(String::hashCode)));
System.out.println(result); // {1=97, 2=6304, 3=99300}

 *summarizingInt() 
 *summarizingLong() 
 *summarizingDouble() 
List<String> strings = List.of("a", "bb", "cc", "ddd");
Map<Integer, IntSummaryStatistics> result = strings.stream().collect(groupingBy(String::length, summarizingInt(String::hashCode)));
System.out.println(result);

//返回结果:
{
    1=IntSummaryStatistics{
      count=1, 
      sum=97, 
      min=97, 
      average=97.000000, 
      max=97}, 
    2=IntSummaryStatistics{
      count=2, 
      sum=6304, 
      min=3136, 
      average=3152.000000, 
      max=3168}, 
    3=IntSummaryStatistics{
      count=1, 
      sum=99300, 
      min=99300, 
      average=99300.000000, 
      max=99300}
}

Example7

// group reducing  分组换算
List<String> strings = List.of("a", "bb", "cc", "ddd");
Map<Integer, List<Character>> result = strings.stream()
  .map(toStringList())
  .collect(groupingBy(List::size, reducing(List.of(), (l1, l2) -> Stream.concat(l1.stream(), l2.stream())
    .collect(Collectors.toList()))));
System.out.println(result); // {1=[a], 2=[b, b, c, c], 3=[d, d, d]}

Example8

//分组 使用Collectors 获取max、min
List<String> strings = List.of("a", "bb", "cc", "ddd");
Map<Integer, Optional<String>> result = strings.stream().collect(groupingBy(String::length, Collectors.maxBy(Comparator.comparing(String::toUpperCase))));
System.out.println(result); // {1=Optional[a], 2=Optional[cc], 3=Optional[ddd]}

Example9

//分组获得长度大于1的字符串 作为一个新的TreeSet
var result = strings.stream()
  .collect(
    groupingBy(String::length,
      mapping(String::toUpperCase,
        filtering(s -> s.length() > 1,
          toCollection(TreeSet::new)))));
//result
{1=[], 2=[BB, CC], 3=[DDD]}

Example10

//字符串列表,按照它们的匹配长度对它们进行分组仅保留具有非零长度的不同元素,最终格式化
var result = strings.stream()
  .collect(
    groupingBy(String::length,
      mapping(toStringList(),
        flatMapping(s -> s.stream().distinct(),
          filtering(s -> s.length() > 0,
            mapping(String::toUpperCase,
              reducing("", (s, s2) -> s + s2)))))
    ));
//result 
{1=A, 2=BC, 3=D}

猜你喜欢

转载自blog.csdn.net/u011663149/article/details/87883739