一:循环遍历list集合的四种方式

  • 简单for循环

  • iterator循环

  • 增加for循环

  • Lanbda表达式

二:四种遍历方式的用法示例

//简单for循环 
List<SalaryAdjustmentFile> fileList = new ArrayList<>();
fileList.add(new SalaryAdjustmentFile());
//此处省略加入list元素
for(int i = 0; n < fileList.size(); i++){
  .....//此处省略具体实现方法
}

//iterator循环
Iterator<SalaryAdjustmentFile> iter = fileList.iterator();
while(iter.hasNext()){
.....//此处省略具体实现方法
}

// 增强for循环
for(SalaryAdjustmentFile salaryAdjustmentFile : fileList){
.....//此处省略具体实现方法
}

//Lanbda表达式
fileList.stream().forEach(salaryAdjustmentFile -> {
.....//此处省略具体实现方法
});

三:删除集合元素

因为自己代码中是使用Lanbda表达式实现的list集合遍历,所以此处只展示这种方式遍历集合删除元素的功能
fileList.stream().forEach(salaryAdjustmentFile -> {
String staffId = salaryAdjustmentFile.getStaffId();
String modelFieldName = salaryAdjustmentFile.getModelFieldName();
String tenantId = BaseContextHandler.getTenantId();
String salaryPlanId = salaryAdjustmentFile.getSalaryPlanId();
SalaryAdjustmentFile last = salaryAdjustmentFileMapper.selectLastTimeFile(staffId,modelFieldName,salaryPlanId,tenantId);
Map map = salaryFileMapper.selectItemValueByStaffId(staffId,modelFieldName,salaryPlanId,tenantId);
if(StringUtils.isEmpty(last) && !map.isEmpty()){
salaryAdjustmentFile.setValueBeforeAdjustment((BigDecimal) map.get("itemValue"));
}else {
salaryAdjustmentFile.setValueBeforeAdjustment(last.getValueAfterAdjustment());
}
salaryAdjustmentFile.creat(idWorker.nextStringId());
//如果满足下面条件则删除元素
if((salaryAdjustmentFile.getValueBeforeAdjustment().subtract(salaryAdjustmentFile.getValueAfterAdjustment()) == BigDecimal.ZERO)){
fileList.remove(salaryAdjustmentFile);
}
});

上面的代码在运行的时候,并不会如我们期望的一样删除list元素成功,而是控制台会报错java.lang.NullPointerException: null

报错原因分析:
经过百度搜索了解到,这是并发修改异常错误,是集合遍历原理导致的,具体原因是这样的:

不管是哪种方式的集合遍历方法,当我们在遍历某个集合的时候,Collection的实现并没有同步化,如果在多线程应用程序中出现同时访问,而且出现修改操作的时候都要求外部操作同步化;调用遍历操作获得的遍历对象在多线程修改集合的时候也自动失效,并抛出java.util.ConcurrentModificationException。这种实现机制是fail-fast,对外部 的修改并不能提供任何保证。遍历对象在被创建的时候,同时创建了一张单链的索引表,指针指向原始数据对象,只能顺序读取,不能逆向操作,而set、list等集合是动态、可变的数据结构;当原始对象改变时,索引并为改变,因此,索引指针继续移动的时候,找不到要迭代的对象就会报错。

四:针对第三步中错误的解决方案

将删除对象放在一个临时的集合中,最后执行removeAll方法移除,如下:

List<SalaryAdjustmentFile> removeList = new ArrayList<>();
fileList.stream().forEach(salaryAdjustmentFile -> {
String staffId = salaryAdjustmentFile.getStaffId();
String modelFieldName = salaryAdjustmentFile.getModelFieldName();
String tenantId = BaseContextHandler.getTenantId();
String salaryPlanId = salaryAdjustmentFile.getSalaryPlanId();
SalaryAdjustmentFile last = salaryAdjustmentFileMapper.selectLastTimeFile(staffId,modelFieldName,salaryPlanId,tenantId);
Map map = salaryFileMapper.selectItemValueByStaffId(staffId,modelFieldName,salaryPlanId,tenantId);
if(StringUtils.isEmpty(last) && !map.isEmpty()){
salaryAdjustmentFile.setValueBeforeAdjustment((BigDecimal) map.get("itemValue"));
}else {
salaryAdjustmentFile.setValueBeforeAdjustment(last.getValueAfterAdjustment());
}
salaryAdjustmentFile.creat(idWorker.nextStringId());
//如果满足下面条件则删除元素
if((salaryAdjustmentFile.getValueBeforeAdjustment().subtract(salaryAdjustmentFile.getValueAfterAdjustment()) == BigDecimal.ZERO)){
removeList.add(salaryAdjustmentFile);
}
});
//删除掉集合中满足删除条件的数据
fileList.removeAll(removeList);

Lambda 表达式遍历集合时用remove方法删除list集合中满足条件的元素问题的更多相关文章

  1. java 数据类型:集合接口Collection之常用ArrayList;lambda表达式遍历;iterator遍历;forEachRemaining遍历;增强for遍历;removeIf批量操作集合元素(Predicate);

    java.util.Collection接口 Java的集合主要由两个接口派生出来,一个是Collection一个是Map,本章只记录Collection常用集合 集合只能存储引用类型数据,不能存储基 ...

  2. Java Map集合 遍历 五种方式(包含 Lambda 表达式遍历)

    示例代码如下: package com.miracle.luna.lambda; import java.util.HashMap; import java.util.Iterator; import ...

  3. Java List集合 遍历 四种方式(包含 Lambda 表达式遍历)

    示例代码如下: package com.miracle.luna.lambda; import java.util.ArrayList; import java.util.List; /** * @A ...

  4. Java Array数组 遍历 四种方式(包含 Lambda 表达式遍历)

    示例代码如下: package com.miracle.luna.lambda; import java.util.Arrays; /** * @Author Miracle Luna * @Date ...

  5. C#中分别对委托、匿名方法、Lambda表达式、Lambda表达式树以及反射执行同一方法的过程进行比较。

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. 【转载】C#中List集合使用LastOrDefault方法查找出最后一个符合条件的元素

    在C#的List集合中,FirstOrDefault方法一般用来查找List集合中第一个符合条件的对象,如果未查到则返回相应默认值.其实如果要查找最后一个符合条件的List集合元素对象,可以使用Las ...

  7. 【转载】C#通过Remove方法移除DataTable中的某一列数据

    在C#中的Datatable数据变量的操作过程中,有时候我们需要移除当前DataTable变量中的某一列的数据,此时我们就需要使用到DataTable变量内部的Columns属性变量的Remove方法 ...

  8. 在Python的列表中利用remove()方法删除元素的教程

    在Python的列表中利用remove()方法删除元素的教程 这篇文章主要介绍了在Python的列表中利用remove()方法删除元素的教程,是Python入门中的基础知识,注意其和pop()方法的区 ...

  9. Lambda表达式遍历集合

    1.Collection Java 8 为Iterable接口新增了一个forEach(Consumer action)默认方法,该方法所需参数的类型是一个函数式接口,而Iterable接口是Coll ...

随机推荐

  1. Python—变量,条件语句,while循环,运算符,字符串等

     Python初识以及变量: 变量名:——字母 ——数字 ——下划线[见名识意] (PS:数字不能开头:不能是关键字:最好不能和python内置的东西重复) ##################### ...

  2. windows tcp server select

    #include <stdio.h> #include <tchar.h> #include <winsock2.h> #include <iostream& ...

  3. Elasticsearch系列---生产数据备份恢复方案

    前言 生产环境中运行的组件,只要有数据存储,定时备份.灾难恢复是必修课,mysql数据库的备份方案已经非常成熟,Elasticsearch也同样有成熟的数据备份.恢复方案,我们来了解一下. 概要 本篇 ...

  4. 《刻意练习之C#》-0016- C#预处理器指令

    预处理指令 这些指令/命令不会转换为可执行代码,但会影响编译过程的各个方面:列如,可以让编译器不编译某一部分代码等. C#中主要的预处理指令 #define和#undef #define指令定义: # ...

  5. 安装并配置Samba

    1. 安装 samba ~$sudo apt-get install samba 2. 修改 samba 的配置文件 ~$sudo gedit /etc/samba/smb.conf 添加如下内容 [ ...

  6. SpringColud Eureka的服务注册与发现

    一.Eureka简介 本文中所有代码都会上传到git上,请放心浏览 项目git地址:https://github.com/839022478/Spring-Cloud 在传统应用中,组件之间的调用,通 ...

  7. yaml读取封装

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ 对yaml格式的配置文件的操作 """ ...

  8. Android学习笔记使用Notication 显示通知

    实现步骤 代码实现 创建MainActivity和DetailActivity(点击通知后要跳转的Activity),两个Activity的布局文件就是添加一张全屏的背景图,老规矩,不粘贴. Main ...

  9. Flutter学习笔记(32)--PointerEvent事件处理

    如需转载,请注明出处:Flutter学习笔记(32)--PointerEvent事件处理 在Android原生的开发中,对于事件的处理,我们都知道事件分为down.move.up事件,对于ViewGr ...

  10. 8.实战交付一套dubbo微服务到k8s集群(1)之Zookeeper部署

    1.基础架构 主机名 角色 ip HDSS7-11.host.com K8S代理节点1,zk1 10.4.7.11 HDSS7-12.host.com K8S代理节点2,zk2 10.4.7.12 H ...