您好,登錄后才能下訂單哦!
本篇內容介紹了“使用stream的Collectors.toMap()方法常見問題如何解決”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
java8開始的流式編程很大程度上簡化了我們的代碼,提高了開發效率。
我們經常會使用到stream的Collectors.toMap()來將List轉換Map
1、java.lang.IllegalStateException: Duplicate key
2、java.lang.NullPointerException
第一個是由于在List轉Map過程中Map集合的key重復導致的;
第二個是由于在List轉Map過程中Map集合的value有null導致的(當存在value值為空時,使用Collectors.toMap()會報NPE,因為底層調用了Map的merge方法,而map方法規定了此處的vlue不能為null,從而拋出空指針異常);
1、Collectors.toMap(dto ->key值 , dto -> dto,(v1,v2) -> v1)
在后面添加(v1,v2)->v1 指定選取第一個值 當key值重復的時候,根據情況而定選取第一個還是第二個)
2、自定義一個Map來接收,不使用Collectors.toMap()
第一種情況示例:
import com.google.common.collect.Lists; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import lombok.Data; public class Test { private static List<User> userList = Lists.newArrayList(); @Data public static class User { private String userCode; private String userName; } /** * 初始化數據 * (這里的userCode=10002重復) */ public static void initData() { User user1 = new User(); user1.setUserCode("10001"); user1.setUserName("張三"); User user2 = new User(); user2.setUserCode("10002"); user2.setUserName("李四"); User user3 = new User(); user3.setUserCode("10002"); user3.setUserName("王五"); userList.add(user1); userList.add(user2); userList.add(user3); } public static void main(String[] args) { initData(); //反例 // Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getUserCode, User::getUserName)); //正例,在后面添加(u1,u2)->u1 指定選取第一個值 當key值重復的時候,根據情況而定選取第一個還是第二個 Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getUserCode, User::getUserName, (u1, u2) -> u1)); System.out.println(userMap); } }
第二種情況示例:
import com.google.common.collect.Lists; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import lombok.Data; public class Test { private static List<User> userList = Lists.newArrayList(); @Data public static class User { private String userCode; private String userName; } /** * 初始化數據 * (這里的userCode=10003的userName為空) */ public static void initData() { User user1 = new User(); user1.setUserCode("10001"); user1.setUserName("張三"); User user2 = new User(); user2.setUserCode("10002"); user2.setUserName("李四"); User user3 = new User(); user3.setUserCode("10003"); user3.setUserName(null); userList.add(user1); userList.add(user2); userList.add(user3); } public static void main(String[] args) { initData(); //反例 // Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getUserCode, User::getUserName)); //正例 (如果對轉換后的順序有要求,這里還可以使用LinkedHashMap) Map<String, String> userMap = userList.stream().collect(HashMap::new, (map, user) -> map.put(user.getUserCode(), user.getUserName()), HashMap::putAll); System.out.println(userMap); } }
List TO Map
List Stream 轉換 Map時向collect()方法中傳遞Collector對象,對象由Collectors.toMap()方法返回。
List<GroupBrandCateBO> list = new ArrayList<>( Arrays.asList( new GroupBrandCateBO("v1", "g1", "b1"), new GroupBrandCateBO("v1", "g1", "b1"), new GroupBrandCateBO("v3", "g3", "b3") ) ); Map<String, String> map = list.stream().collect(Collectors.toMap(item -> item.getVersion(), item -> item.getGroupCode(), (oldVal, currVal) -> oldVal, LinkedHashMap::new)); System.out.println(map.getClass()); Map<String, String> map0 = list.stream().collect(Collectors.toMap(item -> item.getVersion(), item -> item.getGroupCode(), (oldVal, currVal) -> oldVal)); System.out.println(map0.getClass()); System.out.println(map0.toString()); Map<String, String> map1 = list.stream().collect(Collectors.toMap(GroupBrandCateBO::getVersion, GroupBrandCateBO::getGroupCode)); System.out.println(map1.toString());
Console
class java.util.LinkedHashMap
class java.util.HashMap
{v1=g1, v3=g3}
Exception in thread “main” java.lang.IllegalStateException: Duplicate key g1
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
…
toMap()函數重載:
未指定合并函數mergeFunction情況下,傳入throwingMerger()返回BinaryOperator對象,當出現key重復時,調用合并函數!
未指定Supplier實例情況下,默認生成HashMap實例。
public static <T, K, U> Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) { return toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new); } public static <T, K, U> Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction) { return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new); } public static <T, K, U, M extends Map<K, U>> Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier) { BiConsumer<M, T> accumulator = (map, element) -> map.merge(keyMapper.apply(element), valueMapper.apply(element), mergeFunction); return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_ID); } private static <T> BinaryOperator<T> throwingMerger() { return (u,v) -> { throw new IllegalStateException(String.format("Duplicate key %s", u)); }; }
補充
關于合并函數
List<GroupBrandCateBO> list = new ArrayList<>( Arrays.asList( new GroupBrandCateBO("v1", "g1", "b1"), new GroupBrandCateBO("v1", "g2", "b2"), new GroupBrandCateBO("v1", "g2", "b2"), new GroupBrandCateBO("v3", "g3", "b3") ) ); Map<String, String> map00 = list.stream().collect(Collectors.toMap(item -> item.getVersion(), item -> item.getGroupCode(), (oldVal, currVal) -> currVal)); Map<String, String> map01 = list.stream().collect(Collectors.toMap(item -> item.getVersion(), item -> item.getGroupCode(), (oldVal, currVal) -> oldVal + currVal)); System.out.println(map00.toString()); System.out.println(map01.toString());
Console
{v1=g2, v3=g3}
{v1=g1g2g2, v3=g3}
傳入Lambda表達式將轉化為BinaryOperator<U> mergeFunction對象,合并處理value,非Key!!!
比如:
(oldVal, currVal) -> currVal) // key相同時當前值替換原始值 (oldVal, currVal) -> oldVal + currVal //key相同時保留原始值和當前值
“使用stream的Collectors.toMap()方法常見問題如何解決”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。