溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用stream的Collectors.toMap()方法常見問題如何解決

發(fā)布時間:2023-03-06 15:09:49 來源:億速云 閱讀:147 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“使用stream的Collectors.toMap()方法常見問題如何解決”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

    使用stream的Collectors.toMap()方法常見問題

    java8開始的流式編程很大程度上簡化了我們的代碼,提高了開發(fā)效率。

    我們經(jīng)常會使用到stream的Collectors.toMap()來將List轉(zhuǎn)換Map

    在使用過程中有兩個小坑需要注意

    1、java.lang.IllegalStateException: Duplicate key

    2、java.lang.NullPointerException

    第一個是由于在List轉(zhuǎn)Map過程中Map集合的key重復(fù)導(dǎo)致的;

    第二個是由于在List轉(zhuǎn)Map過程中Map集合的value有null導(dǎo)致的(當(dāng)存在value值為空時,使用Collectors.toMap()會報NPE,因為底層調(diào)用了Map的merge方法,而map方法規(guī)定了此處的vlue不能為null,從而拋出空指針異常);

    解決方案

    1、Collectors.toMap(dto ->key值 , dto -> dto,(v1,v2) -> v1)

    在后面添加(v1,v2)->v1 指定選取第一個值 當(dāng)key值重復(fù)的時候,根據(jù)情況而定選取第一個還是第二個)

    2、自定義一個Map來接收,不使用Collectors.toMap()

    使用stream的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;
        }
     
        /**
         * 初始化數(shù)據(jù)
         * (這里的userCode=10002重復(fù))
         */
        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 指定選取第一個值 當(dāng)key值重復(fù)的時候,根據(jù)情況而定選取第一個還是第二個
            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;
        }
     
        /**
         * 初始化數(shù)據(jù)
         * (這里的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));
     
            //正例 (如果對轉(zhuǎn)換后的順序有要求,這里還可以使用LinkedHashMap)
            Map<String, String> userMap = userList.stream().collect(HashMap::new, (map, user) -> map.put(user.getUserCode(), user.getUserName()), HashMap::putAll);
     
            System.out.println(userMap);
        }
     
    }

    Stream ToMap(Collectors.toMap) 實踐

    Requirements

    List TO Map

    List Stream 轉(zhuǎn)換 Map時向collect()方法中傳遞Collector對象,對象由Collectors.toMap()方法返回。

    如下實現(xiàn)List轉(zhuǎn)換為Map

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

    問題分析

    toMap()函數(shù)重載:

    • 未指定合并函數(shù)mergeFunction情況下,傳入throwingMerger()返回BinaryOperator對象,當(dāng)出現(xiàn)key重復(fù)時,調(diào)用合并函數(shù)!

    • 未指定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)); };
    }

    補充

    關(guān)于合并函數(shù)

    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表達式將轉(zhuǎn)化為BinaryOperator<U> mergeFunction對象,合并處理value,非Key?。?!

    比如:

    (oldVal, currVal) -> currVal) // key相同時當(dāng)前值替換原始值
    (oldVal, currVal) -> oldVal + currVal //key相同時保留原始值和當(dāng)前值

    “使用stream的Collectors.toMap()方法常見問題如何解決”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

    向AI問一下細節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI