Stream中關於collect方法的介紹

原文鏈接:https://blog.csdn.net/liujun03/article/details/80701999

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

第一種:

<R, A> R collect(Collector<? super T, A, R> collector);

第二種:

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章