Table of Contents
ToggleJava Collectors toMap Method
使用所提供的靜態方法,傳入 2 個 Function 參數,設定鍵與值轉換成 Map , ToMap Java Collectors 介紹常見的 toMap 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
public static <T, K, U> Collector<T, ?, Map<K, U>> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper) {
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- collector
| +- CollectorsToMapTest.java
單元測試
Collectors ToMap Java 提供設定鍵與值轉換成 Map。
toMap
Collectors ToMap Java 建立一個 List ,增加三個元素,轉換成 Map 。
@Test
public void toMap() {
int expectedSize = 3;
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig");
Function<String, String> key = s -> s.toUpperCase();
Function<String, Integer> length = s -> s.length();
Map<String, Integer> result = list.stream().collect(Collectors.toMap(key, length));
System.out.println(result);
assertEquals(expectedSize, result.size());
result = list.stream().collect(Collectors.toMap(Function.identity(), String::length));
System.out.println(result);
assertEquals(expectedSize, result.size());
Function<Integer, Integer> twice = i -> i * i;
result = list.stream().collect(Collectors.toMap(key, length.andThen(twice)));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
{FIG=3, BLUEBERRY=9, MELON=5}
{Fig=3, Blueberry=9, Melon=5}
{FIG=9, BLUEBERRY=81, MELON=25}
toMapThrowException
Java Collectors ToMap Duplicate Key 建立一個 List ,增加五個元素,轉換成 Map ,當有重複鍵時,會拋出例外。
@Test(expected = IllegalStateException.class)
public void toMapThrowException() {
int expectedSize = 3;
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Blueberry", "Melon");
Function<String, String> key = s -> s.toUpperCase();
Function<String, Integer> length = s -> s.length();
Map<String, Integer> result = list.stream().collect(Collectors.toMap(key, length));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
java.lang.IllegalStateException: Duplicate key 9
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1255)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.ruoxue.java_147.collector.CollectorsToMapTest.toMapThrowException(CollectorsToMapTest.java:42)
toMapWithDuplicateKey
Java Collectors ToMap Duplicate Key 建立一個 List ,增加五個元素,轉換成 Map ,當有重複鍵時,傳入 BinaryOperator 參數,解決重複鍵拋出例外的問題。
@Test
public void toMapWithDuplicateKey() {
int expectedSize = 3;
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Blueberry", "Melon");
Function<String, String> key = s -> s.toUpperCase();
Function<String, Integer> length = s -> s.length();
Map<String, Integer> result = list.stream()
.collect(Collectors.toMap(key, length, (oldValue, newValue) -> oldValue));
System.out.println(result);
assertEquals(expectedSize, result.size());
result = list.stream()
.collect(Collectors.toMap(Function.identity(), String::length, (oldValue, newValue) -> oldValue));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
{FIG=3, BLUEBERRY=9, MELON=5}
{Fig=3, Blueberry=9, Melon=5}
toMapTreeMap
Collectors ToMap Java 建立一個 List ,增加五個元素,指定轉換成 TreeMap 。
@Test
public void toMapTreeMap() {
int expectedSize = 3;
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Blueberry", "Melon");
Function<String, String> key = s -> s.toLowerCase();
Function<String, Integer> length = s -> s.length();
Function<Integer, Integer> twice = i -> i * i;
Map<String, Integer> result = list.stream()
.collect(Collectors.toMap(key, length.andThen(twice), (oldValue, newValue) -> oldValue, TreeMap::new));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
{blueberry=81, fig=9, melon=25}
CollectorsToMapTest.java
Collectors ToMap Example 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.collector;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.junit.Test;
public class CollectorsToMapTest {
@Test
public void toMap() {
int expectedSize = 3;
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig");
Function<String, String> key = s -> s.toUpperCase();
Function<String, Integer> length = s -> s.length();
Map<String, Integer> result = list.stream().collect(Collectors.toMap(key, length));
System.out.println(result);
assertEquals(expectedSize, result.size());
result = list.stream().collect(Collectors.toMap(Function.identity(), String::length));
System.out.println(result);
assertEquals(expectedSize, result.size());
Function<Integer, Integer> twice = i -> i * i;
result = list.stream().collect(Collectors.toMap(key, length.andThen(twice)));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
@Test(expected = IllegalStateException.class)
public void toMapThrowException() {
int expectedSize = 3;
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Blueberry", "Melon");
Function<String, String> key = s -> s.toUpperCase();
Function<String, Integer> length = s -> s.length();
Map<String, Integer> result = list.stream().collect(Collectors.toMap(key, length));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
@Test
public void toMapWithDuplicateKey() {
int expectedSize = 3;
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Blueberry", "Melon");
Function<String, String> key = s -> s.toUpperCase();
Function<String, Integer> length = s -> s.length();
Map<String, Integer> result = list.stream()
.collect(Collectors.toMap(key, length, (oldValue, newValue) -> oldValue));
System.out.println(result);
assertEquals(expectedSize, result.size());
result = list.stream()
.collect(Collectors.toMap(Function.identity(), String::length, (oldValue, newValue) -> oldValue));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
@Test
public void toMapTreeMap() {
int expectedSize = 3;
List<String> list = Arrays.asList("Blueberry", "Melon", "Fig", "Blueberry", "Melon");
Function<String, String> key = s -> s.toLowerCase();
Function<String, Integer> length = s -> s.length();
Function<Integer, Integer> twice = i -> i * i;
Map<String, Integer> result = list.stream()
.collect(Collectors.toMap(key, length.andThen(twice), (oldValue, newValue) -> oldValue, TreeMap::new));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
}
心得分享
Collectors ToMap Java 提供了幾種 Collectors 常見方法的操作範例,在應用上相當廣泛,熟悉 Collectors ToMap Example 這些方法的操作,例如: toMap 等方法,可以讓程式碼更加地簡潔及容易維護。