Lambda 表达式遍历集合时用remove方法删除list集合中满足条件的元素问题
一:循环遍历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集合中满足条件的元素问题的更多相关文章
- java 数据类型:集合接口Collection之常用ArrayList;lambda表达式遍历;iterator遍历;forEachRemaining遍历;增强for遍历;removeIf批量操作集合元素(Predicate);
java.util.Collection接口 Java的集合主要由两个接口派生出来,一个是Collection一个是Map,本章只记录Collection常用集合 集合只能存储引用类型数据,不能存储基 ...
- Java Map集合 遍历 五种方式(包含 Lambda 表达式遍历)
示例代码如下: package com.miracle.luna.lambda; import java.util.HashMap; import java.util.Iterator; import ...
- Java List集合 遍历 四种方式(包含 Lambda 表达式遍历)
示例代码如下: package com.miracle.luna.lambda; import java.util.ArrayList; import java.util.List; /** * @A ...
- Java Array数组 遍历 四种方式(包含 Lambda 表达式遍历)
示例代码如下: package com.miracle.luna.lambda; import java.util.Arrays; /** * @Author Miracle Luna * @Date ...
- C#中分别对委托、匿名方法、Lambda表达式、Lambda表达式树以及反射执行同一方法的过程进行比较。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 【转载】C#中List集合使用LastOrDefault方法查找出最后一个符合条件的元素
在C#的List集合中,FirstOrDefault方法一般用来查找List集合中第一个符合条件的对象,如果未查到则返回相应默认值.其实如果要查找最后一个符合条件的List集合元素对象,可以使用Las ...
- 【转载】C#通过Remove方法移除DataTable中的某一列数据
在C#中的Datatable数据变量的操作过程中,有时候我们需要移除当前DataTable变量中的某一列的数据,此时我们就需要使用到DataTable变量内部的Columns属性变量的Remove方法 ...
- 在Python的列表中利用remove()方法删除元素的教程
在Python的列表中利用remove()方法删除元素的教程 这篇文章主要介绍了在Python的列表中利用remove()方法删除元素的教程,是Python入门中的基础知识,注意其和pop()方法的区 ...
- Lambda表达式遍历集合
1.Collection Java 8 为Iterable接口新增了一个forEach(Consumer action)默认方法,该方法所需参数的类型是一个函数式接口,而Iterable接口是Coll ...
随机推荐
- Python快速入门文档
前言 此文本质为本人学习Python过程中的笔记,部分地方叙述表达可能不够清晰,欢迎留言. (本文适合有一定程序语言基础的读者阅读(最好是c语言)) 一.基本语法 1.框架: (1)以缩进表示层次所属 ...
- 数据结构与算法-python描述-单链表
# coding:utf-8 # 单链表的相关操作: # is_empty() 链表是否为空 # length() 链表长度 # travel() 遍历整个链表 # add(item) 链表头部添加元 ...
- 利用struts2进行单个文件,批量文件上传,ajax异步上传以及下载
利用struts2进行单个文件,批量文件上传,ajax异步上传以及下载 1.页面显示代码 <%@ page language="java" import="java ...
- [web][学习随笔]php中http post&get数据传输
GET <!--客户端发送--> <form id="form1" action="doGet.php" method="get&q ...
- while or if
多线程 wait && notifyAll 模式实现时,如果 锁中有判断,对共享对象有curd 操作时,有可能出现异常 即,判断 条件 这个时候关键字有 if 改为while 即可 ...
- 重学 Java 设计模式:实战外观模式「基于SpringBoot开发门面模式中间件,统一控制接口白名单场景」
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 你感受到的容易,一定有人为你承担不容易 这句话更像是描述生活的,许许多多的磕磕绊绊总 ...
- git clone 时注意点
环境: 在公司访问外网需要设置代理,另外,在公司局域网内架设了一台 GIT 服务器. 在使用 git clone 时,不能设置成 git 使用代理: git config --global http. ...
- Python函数&异常处理
1. 函数基础 1.1 参数和返回值 1.1.1 参数 位置参数.关键字参数 def my_func1(x, y, z): print(x+y+z, "计算结束") my_func ...
- Mac搭建Fluter应用环境
1.创建一个路径.例如我创建是: /Users/chenghui/ 然后创建一个文件夹: development 把下载好的Fluter 解压到当前目录下: development /Users/ch ...
- C#数据结构与算法系列(八):栈(Stack)
1.介绍 栈是一个先入后出(FILO-First In Last Out)的有序列表 栈是限制线性表中元素的插入和删除只能在线性表的同一端进行的特殊线性表.允许插入和删除的一端,为变化的一端,称为栈顶 ...