Java 8 introduced Lambdas, which allow us to efficiently remove all elements from a List. The following removes all instances of 2 from myList.
List<Integer> myList;
...
myList.removeIf(x -> x==2);
If I wanted to remove N number of elements (in this case, three), I would use a for loop.
for (int i = 0; i < 3; i++) {
myList.remove(Integer.valueOf(2));
}
Is there a way to remove a specified number of elements from a List using Lambdas? If so, is it more efficient than the for loop code?