Java 8 - for everyone and removeIf

I am trying to perform an operation using ForEachin Java 8 by combining a method removeIf. But I get an error message.

I can not combine ForEachand removeIfin the following program:

public class ForEachIterator {

    public static void main(String[] args) {
        List<Integer> ints = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++) {
            ints.add(i);
        }
        System.out.println(ints);
        // Getting the Error in next line
        ints.forEach(ints.removeIf(i -> i%2 ==0));
        System.out.println(ints);
    }
}
+6
source share
1 answer

For forEachno need, a lambda expression will work on all elements of the set

ints.removeIf(i -> i%2==0)

removeIf : "Removes all elements of this collection that satisfy this predicate"

Simply...

(i) (ints) , (removeIf) (i%2==0) true. true, - .

+25

Source: https://habr.com/ru/post/1625835/


All Articles