Java 8 lambda从列表中获取和删除元素 [英] Java 8 lambda get and remove element from list
本文介绍了Java 8 lambda从列表中获取和删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一个元素列表,我想获得具有给定属性和的元素从列表中删除它。我找到的最佳解决方案是:
Given a list of elements, I want to get the element with a given property and remove it from the list. The best solution I found is:
ProducerDTO p = producersProcedureActive
.stream()
.filter(producer -> producer.getPod().equals(pod))
.findFirst()
.get();
producersProcedureActive.remove(p);
是否可以在lambda表达式中组合get和remove?
Is it possible to combine get and remove in a lambda expression?
推荐答案
从列表中删除元素
objectA.removeIf(x - >条件);
objectA.removeIf(x -> conditions);
例如:
objectA.removeIf(x - > blockedWorkerIds.contains(x));
eg: objectA.removeIf(x -> blockedWorkerIds.contains(x));
List<String> str1 = new ArrayList<String>();
str1.add("A");
str1.add("B");
str1.add("C");
str1.add("D");
List<String> str2 = new ArrayList<String>();
str2.add("D");
str2.add("E");
str1.removeIf(x -> str2.contains(x));
str1.forEach(System.out::println);
输出:
A
B
C
OUTPUT: A B C
这篇关于Java 8 lambda从列表中获取和删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文