在使用Java8 lambda 将List转换为Map时,遇到报错:IllegalStateException- Duplicate key ....
具体报错信息如下:
-
- java.lang.IllegalStateException: Duplicate key com.runcode.springboottourist.lambda.ListToMap2$Person@4fccd51b
-
- at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
- at java.util.HashMap.merge(HashMap.java:1245)
- 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:1374)
- 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 com.runcode.springboottourist.lambda.ListToMap2.demoExp(ListToMap2.java:41)
- 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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
- 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:69)
- at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
- at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
- at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
-
- static class Person{
- private Integer id ; //Id
- private String name ; // 名称
- private String gender; // 性别, 1:男;0:女
- // ignore getter / setter
- }
- final List
list = Lists.newArrayList(); -
- @Before
- public void init(){
- list.add(new Person( 1,"小明","1"));
- list.add(new Person(2,"小红","0"));
- list.add(new Person(3,"小花","0"));
- System.out.println("初始化数据完成,list.size="+list.size());
- }
- /**
- * Description: 演示 IllegalStateException: Duplicate key 异常
- * @return void
- * @version v1.0
- * @author wu
- * @date 2022/12/5 15:52
- */
- @Test
- public void demoExp() throws Exception {
- final Map
map = list.stream().collect(Collectors.toMap(Person::getGender, v -> v)); - System.out.println(map);
- }
在使用lambda进行 list转换为map过程中,遇到 key重复的情况下,就会抛出异常 IllegalStateException- Duplicate key ...
- @Test
- public void fixToMapExp() throws Exception {
- // 办法一: 若冲突,保留前者
- final Map
map = list.stream().collect(Collectors.toMap(Person::getGender, v -> v, (v1, v2) -> v1)); - System.out.println(JSON.toJSONString(map));
-
- // 办法二:若冲突,保留后者
- final Map
map2 = list.stream().collect(Collectors.toMap(Person::getGender, Function.identity(), (v1, v2) -> v2)); - System.out.println(JSON.toJSONString(map2));
- }
1、在使用 lambda 进行list转换map时,要注意 可能会出现key重复的情况,日常开发中,使用 Collectors.toMap 重载的 mergeFunction 方法 ,可以从源头上避免 key 重复的情况。
2、也可以使用 Collectors.groupingBy 方法,直接转换为 对应的 List ,根据实际需求灵活处理即可。
-
- @Test
- public void fixGrouping() throws Exception {
- final Map
> map = list.stream().collect(Collectors.groupingBy(Person::getGender)); - System.out.println("按照 gender 分组:" + JSON.toJSONString(map));
-
- final Map
> map2 = list.stream().collect(Collectors.groupingBy(Person::getGender, Collectors.toMap(Person::getName, v -> v))); - System.out.println("按照 gender 分组 map2 :" + JSON.toJSONString(map2));
- }
更多关于 List 集合的知识:
Java 集合分页 Java List集合分页 List 分页 Java List集合笛卡尔积_HaHa_Sir的博客-CSDN博客_集合分页
Java List排序 java ListMap 排序 Java listmap 模拟 oracle 排序 Java listmap 模拟 mysql 排序_HaHa_Sir的博客-CSDN博客
两个List循环效率对比 List转Map 循环效率对比 Listmap 循环 效率对比_HaHa_Sir的博客-CSDN博客
Java List集合排序 Java8 List集合排序方法 Java Lambda集合排序_HaHa_Sir的博客-CSDN博客_java8 集合排序