The Javadoc of removeIf() states:
Removes all of the elements of this collection that satisfy the given predicate.
The predicate in your example is always true because you map each integer i in your list to trueby the expression: i -> true.
I added a simpler example which removes all even integers and keeps all odd integers by the predicate i % 2 == 0:
Ugly setup:
List<List<Integer>> lists = new ArrayList<List<Integer>>() {{
add(new ArrayList<>(Arrays.asList(1,2,3,4)));
add(new ArrayList<>(Arrays.asList(2,4,6,8)));
add(new ArrayList<>(Arrays.asList(1,3,5,7)));
}};
Keep only odd numbers:
for (List<Integer> list : lists) {
list.removeIf(i -> i % 2 == 0);
System.out.println(list);
}
Output:
[1, 3]
[]
[1, 3, 5, 7]
Answer from Harmlezz on Stack OverflowVideos
The Javadoc of removeIf() states:
Removes all of the elements of this collection that satisfy the given predicate.
The predicate in your example is always true because you map each integer i in your list to trueby the expression: i -> true.
I added a simpler example which removes all even integers and keeps all odd integers by the predicate i % 2 == 0:
Ugly setup:
List<List<Integer>> lists = new ArrayList<List<Integer>>() {{
add(new ArrayList<>(Arrays.asList(1,2,3,4)));
add(new ArrayList<>(Arrays.asList(2,4,6,8)));
add(new ArrayList<>(Arrays.asList(1,3,5,7)));
}};
Keep only odd numbers:
for (List<Integer> list : lists) {
list.removeIf(i -> i % 2 == 0);
System.out.println(list);
}
Output:
[1, 3]
[]
[1, 3, 5, 7]
removeIf will go through each element in your list and run the specified predicate (boolean function) on it. If the predicate returns true, it will be removed from the list. If the predicate returns false, it will not.
In your case, every element will result in the predicate returning true, thus clearing the list.
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
Although the thread is quite old, still thought to provide solution - using Java8.
Make the use of removeIf function. Time complexity is O(n)
producersProcedureActive.removeIf(producer -> producer.getPod().equals(pod));
API reference: removeIf docs
Assumption: producersProcedureActive is a List
NOTE: With this approach you won't be able to get the hold of the deleted item.
There's no need for the forEach, the Lambda expression will work on all elements of the set
ints.removeIf(i -> i%2==0)
removeIf: "Removes all of the elements of this collection that satisfy the given predicate"
Simply...
For each element (i) in the set (ints), remove it if (removeIf) the predicate (i%2==0) is true for this element (i). This will act on the original set and return true if any elements where removed.
the method removeif itself gives elements one by one as its passing parameter that's why we don't need to use forEach loop here.
over ints ->
ints.forEach(ints.lambdaEx)
it is causing two n loops causing n^2 iteration which is not needed here.
Just use "removeif( n -> n % 2 == 0)"
Predicate<Student> condition = p->p.grade=='C'&& p.grade=='B';
students.removeIf(condition);
Should be
Predicate<Student> condition = p->p.grade=='C'|| p.grade=='B';
students.removeIf(condition);
It's impossible for a student's grade to be both B and C, so the predicate you provided will never be true.
Also, it's possible to chain together two predicates with the and() and or() methods like so:
Predicate<Student> condition = p->p.grade=='C'
Predicate<Student> condition2 = p->p.grade=='B';
students.removeIf(condition.or(condition2);
// students.removeIf(condition.and(condition2); works similarly but with logical and
https://howtodoinjava.com/java8/predicates-logical-operations/
The issue is with this line:
Predicate<Student> condition = p->p.grade=='C'&& p.grade=='B';
You'r asking to remove a student that has grade 'C' AND 'B'. It should be 'C' OR 'B'.
Replace with this:
Predicate<Student> condition = p->p.grade=='C' || p.grade=='B';