📜  Java中的收集器 collectAndThen() 方法及示例

📅  最后修改于: 2022-05-13 01:55:06.945000             🧑  作者: Mango

Java中的收集器 collectAndThen() 方法及示例

Java中类收集器的collectingAndThen(Collector下游, 函数 finisher)方法,它采用了Collector ,这样我们就可以进行额外的整理变换。

句法 :

public static  
       Collector  
       collectingAndThen(Collector  downstream, 
                         Function  finisher)
                                                             

Where,
    
  • T : 输入元素的类型
  • A :下游收集器的中间堆积类型
  • R : 下游收集器的结果类型
  • RR :结果收集器的结果类型

    参数:此方法接受下面列出的两个参数

  • 下游:它是一个收集器的实例,即我们可以在这里使用任何收集器。
  • Finisher:它是一个函数的实例,将应用于下游收集器的最终结果。

    Returns:返回一个收集器,它执行下游收集器的操作,然后在 Finisher函数的帮助下进行额外的整理步骤。

    下面是说明collectAndThen()方法的示例。

    示例 1:创建不可变列表

    // Write Java code here
    // Collectors collectingAndThen() method
      
    import java.util.Collections;
    import java.util.List;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
      
    public class GFG {
        public static void main(String[] args)
        {
            // Create an Immutable List
            List lt
                = Stream
                      .of("GEEKS", "For", "GEEKS")
                      .collect(Collectors
                                   .collectingAndThen(
                                       Collectors.toList(),
                                       Collections:: unmodifiableList));
            System.out.println(lt);
        }
    }
    
    输出:
    [GEEKS, For, GEEKS]
    

    示例 2:创建一个不可变集合。

    // Write Java code here
    import java.util.Collections;
    import java.util.List;
    import java.util.Set;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
      
    public class GFG {
        public static void main(String[] args)
        {
            // Create an Immutable Set
            Set st
                = Stream
                      .of("GEEKS", "FOR", "GEEKS")
                      .collect(
                          Collectors
                              .collectingAndThen(Collectors.toSet(),
                                                 Collections::
                                                     unmodifiableSet));
            System.out.println(st);
        }
    }
    
    输出:
    [GEEKS, FOR]
    

    示例 2:创建不可变映射

    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // Create an Immutable Map
            Map mp
                = Stream
                      .of(new String[][] {
                          { "1", "Geeks" },
                          { "2", "For" },
                          { "3", "Geeks" } })
                      .collect(
                          Collectors
                              .collectingAndThen(
                                  Collectors.toMap(p -> p[0], p -> p[1]),
                                  Collections::
                                      unmodifiableMap));
            System.out.println(mp);
        }
    }
    
    输出:
    {1=Geeks, 2=For, 3=Geeks}
    

    注意:此方法最常用于创建不可变集合。