快速解决异常 java.util.ConcurrentModificationException: null

异常原因

  • 有一串代码逻辑如下

    if (serviceRule.getId() != null){
        for (OrderServiceRule orderServiceRule : osrList) {
            if (serviceRule.getId() != orderServiceRule.getId()){
                osrList.remove(orderServiceRule);
            }
        }
    }
    

迭代的时候不要直接从迭代的list里移除object。

解决方式

  • 使用filter过滤
    if (serviceRule.getId() != null){
        osrList = osrList.stream().filter(e -> {
            return serviceRule.getId().equals(e.getId());
        }).collect(Collectors.toList());
    }
    
posted @ 2020-09-17 14:40  LanceLi  阅读(494)  评论(0编辑  收藏  举报