Listnumbers = new ArrayList<>(Arrays.asList(5, 10, 15, 20, 25)); numbers.removeIf(n -> n > 10); System.out.println(numbers); // Output: [5, 10]
ListIn both examples, we are using lambda expressions to specify the condition that needs to be fulfilled to remove the elements. The method returns a boolean value indicating whether any elements were removed or not. The package library for the java.util List removeIf() method is java.util.words = new ArrayList<>(Arrays.asList("apple", "banana", "cherry", "kiwi")); words.removeIf(s -> s.contains("a")); System.out.println(words); // Output: [kiwi]