Java Collector
Java Collector 是專門用來作為 Stream 的 collect 方法的參數,而 Collectors 則提供了多種建立 Collector 方法的工具類,都在 java.util.stream package 中, Collector Learn Java 增加了範例及各種操作方法,透過單元測試來驗證產出結果。
Collector Learning Java
collect 也就是收集器,是 Stream 一種通用從流生成複雜結構的方法,只要將 Collector 傳給 collect 方法,就會轉換成想要的資料結構, Java Collector Course 單獨測試一個程式、過程或方法,進行正確性驗證的測試。
Initialize Collector
使用 Collectors 工具類,建立 Set 、 List 與 Map ,學習各種 Collector 的操作及應用。
Stream<String> stream = Stream.of("Blueberry", "Melon", "Fig", "Blueberry", "Melon");
Set<String> set = stream.collect(Collectors.toSet());
stream = Stream.of("Blueberry", "Melon", "Fig", "Blueberry", "Melon");
List<String> list = stream.collect(Collectors.toList());
Function<String, String> key = s -> s.toUpperCase();
Function<String, Integer> length = s -> s.length();
Map<String, Integer> map = list.stream()
.collect(Collectors.toMap(key, length, (oldValue, newValue) -> oldValue));
[Fig, Blueberry, Melon]
[Blueberry, Melon, Fig, Blueberry, Melon]
{FIG=3, BLUEBERRY=9, MELON=5}
Java Collector Tutorial
- Java Collectors mapping Method
- Collectors mapping in Java with Examples
- Java Collectors reducing Method
- Collectors reducing in Java with Examples
- Java Collectors groupingBy Method
- Collectors groupingBy in Java with Examples
- Java Collectors groupingByConcurrent Method
- Collectors groupingByConcurrent in Java with Examples
- Java Collectors partitioningBy Method
- Collectors partitioningBy in Java with Examples
- Difference Between Collectors groupingBy and partitioningBy in Java
- Collectors groupingBy vs partitioningBy in Java
- Java Collectors joining Method
- Collectors joining in Java with Examples