当对stream生成的map进行remove操作时,代码报错,复现代码如下:
package com.test.testdemo01.service;
import com.test.testdemo01.entity.dto.DemoData;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author : xubin
* @date : 2023/12/14
* @description : 针对字节流生成的map存在移除问题,针对异常情况进行测试及提供解决方案
*/
public class IterationDemo {
/**
* 测试使用字节流会造成报错
*/
@Test
public void test1(){
//数据准备
List demoDataList = new ArrayList<>();
DemoData demoData1 = new DemoData();
demoData1.setName("zhangsan");
demoData1.setSno(123);
DemoData demoData2 = new DemoData();
demoData2.setName("zhangsan");
demoData2.setSno(456);
DemoData demoData3= new DemoData();
demoData3.setName("lisi");
demoData3.setSno(123);
demoDataList.add(demoData1);
demoDataList.add(demoData2);
demoDataList.add(demoData3);
Map> collect = demoDataList.stream().
collect(Collectors.groupingBy(DemoData::getName));
for (String key : collect.keySet()){
if ("lisi".equals(key)){
collect.remove(key);
}
}
}
}
DemoData实体:
package com.test.testdemo01.entity.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import java.util.UUID;
/**
* @author : xubin
* @date : 2022/11/21
* @description :
*/
@Data
public class DemoData {
//设置excel表头名称
@ExcelProperty("学生编号")
private Integer sno;
@ExcelProperty("学生的姓名")
private String name;
public static void main(String[] args) {
System.out.println(UUID.randomUUID()
);
}
}
报错详情:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1445)
at java.util.HashMap$KeyIterator.next(HashMap.java:1469)
at com.test.testdemo01.service.IterationDemo.test1(IterationDemo.java:43)
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:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
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$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
Process finished with exit code -1
代码:
package com.test.testdemo01.service;
import com.alibaba.fastjson.JSON;
import com.test.testdemo01.entity.dto.DemoData;
import com.test.testdemo01.entity.dto.TbOrderSendFilePO;
import org.apache.commons.collections4.map.HashedMap;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author : xubin
* @date : 2023/12/14
* @description : 针对字节流生成的map存在移除问题,针对异常情况进行测试及提供解决方案
*/
public class IterationDemo {
/**
* 使用普通hashmap没有这个问题
*/
@Test
public void test3(){
//数据准备
List demoDataList = new ArrayList<>();
DemoData demoData1 = new DemoData();
demoData1.setName("zhangsan");
demoData1.setSno(123);
DemoData demoData2 = new DemoData();
demoData2.setName("zhangsan");
demoData2.setSno(456);
DemoData demoData3= new DemoData();
demoData3.setName("lisi");
demoData3.setSno(123);
demoDataList.add(demoData1);
demoDataList.add(demoData2);
demoDataList.add(demoData3);
Map> collect = new HashedMap<>();
List demoDataList1 = new ArrayList<>();
demoDataList1.add(demoData1);
demoDataList1.add(demoData2);
List demoDataList2 = new ArrayList<>();
demoDataList2.add(demoData3);
collect.put("zhangsan",demoDataList1);
collect.put("lisi",demoDataList2);
System.out.println(JSON.toJSONString(collect));
for (String key : collect.keySet()){
if ("lisi".equals(key)){
System.out.println("need delete");
collect.remove(key);
}
}
System.out.println(JSON.toJSONString(collect));
}
}
输出结果:
{"zhangsan":[{"name":"zhangsan","sno":123},{"name":"zhangsan","sno":456}],"lisi":[{"name":"lisi","sno":123}]}
need delete
{"zhangsan":[{"name":"zhangsan","sno":123},{"name":"zhangsan","sno":456}]}
Process finished with exit code 0
修改后代码
package com.test.testdemo01.service;
import com.alibaba.fastjson.JSON;
import com.test.testdemo01.entity.dto.DemoData;
import com.test.testdemo01.entity.dto.TbOrderSendFilePO;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author : xubin
* @date : 2023/12/14
* @description : 针对字节流生成的map存在移除问题,针对异常情况进行测试及提供解决方案
*/
public class IterationDemo {
/**
* 使用迭代器能够避免这个问题
*/
@Test
public void test2(){
//数据准备
List demoDataList = new ArrayList<>();
DemoData demoData1 = new DemoData();
demoData1.setName("zhangsan");
demoData1.setSno(123);
DemoData demoData2 = new DemoData();
demoData2.setName("zhangsan");
demoData2.setSno(456);
DemoData demoData3= new DemoData();
demoData3.setName("lisi");
demoData3.setSno(123);
demoDataList.add(demoData1);
demoDataList.add(demoData2);
demoDataList.add(demoData3);
Map> collect = demoDataList.stream().
collect(Collectors.groupingBy(DemoData::getName));
System.out.println(JSON.toJSONString(collect));
Iterator>> iterator1 =
collect.entrySet().iterator();
while (iterator1.hasNext()){
Map.Entry> next = iterator1.next();
if ("lisi".equals(next.getKey())){
System.out.println("need delete");
iterator1.remove();
}
}
System.out.println(JSON.toJSONString(collect));
}
}
输出日志:
{"lisi":[{"name":"lisi","sno":123}],"zhangsan":[{"name":"zhangsan","sno":123},{"name":"zhangsan","sno":456}]}
need delete
{"zhangsan":[{"name":"zhangsan","sno":123},{"name":"zhangsan","sno":456}]}
Process finished with exit code 0