欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決使用stream將list轉(zhuǎn)map時,key重復(fù)導(dǎo)致報錯的問題

 更新時間:2021年06月10日 11:03:03   作者:張財華  
這篇文章主要介紹了解決使用stream將list轉(zhuǎn)map時,key重復(fù)導(dǎo)致報錯的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

要將List對象集合轉(zhuǎn)為map集合,可以通過stream流的形式快速實現(xiàn)轉(zhuǎn)換:

//三個Users對象組成一個List集合
List<Users> list = new ArrayList<>();
list.add(Users.builder().userName("11").userId(1).build());
list.add(Users.builder().userName("11").userId(2).build());
list.add(Users.builder().userName("33").userId(3).build());
//將list轉(zhuǎn)map
Map<String, Users> usersMap = list.stream()
    .collect(Collectors.toMap(Users::getUserName, user -> user));
System.out.println(usersMap.get("11"));

但是上述代碼運行后報了異常:

意思為map中出現(xiàn)了重復(fù)的key,也就是說通過上述方法轉(zhuǎn)map時,出現(xiàn)重復(fù)key并不會出現(xiàn)覆蓋的情況,而是再次在map中添加一個重復(fù)的key,導(dǎo)致報錯。

所以通過stream實現(xiàn)list轉(zhuǎn)map時,要實現(xiàn)重復(fù)的key會被覆蓋,可以使用Function.identity()方法:

//三個Users對象組成一個List集合
List<Users> list = new ArrayList<>();
list.add(Users.builder().userName("11").userId(1).build());
list.add(Users.builder().userName("11").userId(2).build());
list.add(Users.builder().userName("33").userId(3).build());
//將list轉(zhuǎn)map,這里是出現(xiàn)重復(fù)key時,覆蓋前一個
Map<String, Users> usersMap = list.stream()
    .collect(Collectors.toMap(Users::getUserName, Function.identity(), (user1, user2) -> user2));
System.out.println(usersMap.get("11"));
//輸出結(jié)果:
edu.nf.ch08.entity.Users@41aaedaa

JDK 8 Stream List轉(zhuǎn)換為Map的duplicate Key異常

Stream List to Map

Stream提供了List轉(zhuǎn)換為Map提供了非常易用的方法:

Collectors.java:

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

其在轉(zhuǎn)換過程中,會拋出異常:

 @Test(expected = IllegalStateException.class)
    public void testStreamMap_duplicateKey() {
        Employee employee = Employee.builder().id(1).age(20).firstName("zhang").build();
        Employee employee1 = Employee.builder().id(2).age(21).firstName("Li").build();
        Employee employee2 = Employee.builder().id(3).age(22).firstName("Li").build();
        Employee employee3 = Employee.builder().id(4).age(23).firstName("Chen").build();
        List<Employee> employees = Lists.newArrayList();
        employees.add(employee);
        employees.add(employee1);
        employees.add(employee2);
        employees.add(employee3);
        Map<String, Integer> dataMap = employees.stream().collect(Collectors.toMap(e -> e.getFirstName(), e -> e.getAge()));
        //Duplicate Key
        Map<Integer, Employee> employeeMap = employees.stream().collect(Collectors.toMap(e->e.getAge(), e->e));
    }

拋出異常信息:

java.lang.IllegalStateException: Duplicate key 21
 at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
 at java.util.HashMap.merge(HashMap.java:1254)
 at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
 at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
 at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
 at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
 at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
 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.cjf.java.learn.jdk8.StreamTest.testStreamMap_duplicateKey(StreamTest.java:90)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
 at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
 at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
 at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
 at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
 at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Process finished with exit code 255

如何解決

增加重復(fù)key情況下的沖突處理策略:

 Map<String, Integer> dataMap = employees.stream().collect(Collectors.toMap(e -> e.getFirstName(), e -> e.getAge(), (k1, k2)-> k1));
        Assert.assertThat(dataMap, hasKey("zhang"));
        Assert.assertThat(dataMap, hasKey("Li"));
        Assert.assertThat(dataMap, hasKey("Chen"));
        Assert.assertThat(dataMap.keySet(), hasSize(3));
        Map<Integer, Employee> employeeMap = employees.stream().collect(Collectors.toMap(e->e.getAge(), e->e, (k1, k2) -> k1));
        Assert.assertThat(dataMap.keySet(), hasSize(3));

這里的處理策略是:

(k1, k2) -> k1

總結(jié)

在Collectors.toMap()轉(zhuǎn)換過程中,需要注意一下duplicate key的處理邏輯,需要增加mergeFunction()處理方法。以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家

相關(guān)文章

  • java8新特性之接口默認(rèn)方法示例詳解

    java8新特性之接口默認(rèn)方法示例詳解

    這篇文章主要給大家介紹了關(guān)于java8新特性之接口默認(rèn)方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用java8具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Spring?JPA?find分頁示例詳解

    Spring?JPA?find分頁示例詳解

    這篇文章主要為大家介紹了Spring?JPA?find分頁示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Spring動態(tài)配置計時器觸發(fā)時間的實例代碼

    Spring動態(tài)配置計時器觸發(fā)時間的實例代碼

    這篇文章主要介紹了Spring動態(tài)配置計時器觸發(fā)時間的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-06-06
  • Java算法之重新排列數(shù)組例題

    Java算法之重新排列數(shù)組例題

    這篇文章主要介紹了Java算法之重新排列數(shù)組例題,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-08-08
  • MP(MyBatis-Plus)實現(xiàn)樂觀鎖更新功能的示例代碼

    MP(MyBatis-Plus)實現(xiàn)樂觀鎖更新功能的示例代碼

    這篇文章主要介紹了MP(MyBatis-Plus)實現(xiàn)樂觀鎖更新功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java核心編程之文件隨機讀寫類RandomAccessFile詳解

    Java核心編程之文件隨機讀寫類RandomAccessFile詳解

    這篇文章主要為大家詳細(xì)介紹了Java核心編程之文件隨機讀寫類RandomAccessFile,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 淺談web服務(wù)器項目中靜態(tài)請求和動態(tài)請求處理

    淺談web服務(wù)器項目中靜態(tài)請求和動態(tài)請求處理

    這篇文章主要介紹了淺談web服務(wù)器項目中靜態(tài)請求和動態(tài)請求處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 使用java的HttpClient實現(xiàn)多線程并發(fā)

    使用java的HttpClient實現(xiàn)多線程并發(fā)

    這篇文章主要介紹了使用java的HttpClient實現(xiàn)多線程并發(fā)的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • 淺談SpringBoot如何自定義Starters

    淺談SpringBoot如何自定義Starters

    今天帶大家來學(xué)習(xí)SpringBoot如何自定義Starters,文中有非常詳細(xì)的圖文介紹及代碼示例,對正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • SpringBoot使用Jwt處理跨域認(rèn)證問題的教程詳解

    SpringBoot使用Jwt處理跨域認(rèn)證問題的教程詳解

    這篇文章主要介紹了SpringBoot使用Jwt處理跨域認(rèn)證問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06

最新評論