天天看點

Stream中關于collect方法的介紹

在Stream 的API中可以查到有兩種collect方法,分别是:

第一種:

第二種:

<R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator,
        BiConsumer<R, R> combiner);
           

對于第1個方法,我們主要是使用 Collectors(java.util.stream.Collectors)來進行各種 reduction 操作,下面舉幾個例子:

  1. 将數組組成字元串,注:Collectors.joining()有三個重載方法
    String[] arr ={"aa","ccc","sss"};
    System.out.println(Arrays.stream(arr).collect(joining()));
    // aacccsss
    System.out.println(Arrays.stream(arr).collect(joining("|")));
    // aa|ccc|sss
    System.out.println(Arrays.stream(arr).collect(joining(",","{","}")));
    // {aa,ccc,sss}
               
  2. 将數組轉為集合List
    String[] arr ={"aa","ccc","sss"};
    System.out.println(Arrays.stream(arr).collect(toList()));
    
    // [aa, ccc, sss]
               
  3. 将list中的資料分組 并統計數量
    public static class Person{
        private long id;
        private int age;
           private String name;
    
    //   get/set
    }
    
    List<Person> list= Lists.newArrayList();
    //假裝list中已有許多條資料=-=
    Map<Integer, Long> personGroups = list.stream().
                   collect(Collectors.groupingBy(Person::getAge,counting()));
    // 這樣我們就得到了一個 以年齡為key,以這個年齡的人數為value的map了
               

上面應該是幾個比較常用的對第一個collect方法的使用了。下面着重寫一下對第二collect個方法的使用過程。

對于函數

<R> R collect(Supplier<R> supplier,
                  BiConsumer<R, ? super T> accumulator,
                  BiConsumer<R, R> combiner);
           

來說,參數supplier 是一個生成目标類型執行個體的方法,代表着目标容器是什麼;accumulator是将操作的目标資料填充到supplier 生成的目标類型執行個體中去的方法,代表着如何将元素添加到容器中;而combiner是将多個supplier 生成的執行個體整合到一起的方法,代表着規約操作,将多個結果合并。

如上面的儲存有多個Person執行個體的list來說,假如我們需要将這個list做一個轉變,變為以id為key,value為person的Map的話,那就可以使用這個方法了:

Map<Long,Person> personMap =list.stream().collect(Maps::newHashMap,                                                                      (map,p)>map.put(p.getId(),p),Map::putAll);                                      
           

注:lambda表達式的應用在Stream中很重要的。

上面的雙冒号運算符所在的表達式轉化一下就可以得到:

Map<Long,Person> personMap = list.stream().collect(() -> new HashMap<>(), 
                                 (map ,p) ->map.put(p.getId(),p),(m ,n) -> m.putAll(n));
           

繼續閱讀