Java 8 lambda 从列表中获取和删除元素元素、列表中、Java、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 = producersPro...

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);

Is it possible to combine get and remove in a lambda expression?

解决方案 对比讲解 lambda表达式与传统java接口函数实现方式

To Remove element from the list

objectA.removeIf(x -> conditions);

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);

OUTPUT: A B C

阅读全文

相关推荐

最新文章