Listlist1 = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); List list2 = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7)); boolean removed = list1.removeAll(list2); System.out.println("Elements removed: " + removed); System.out.println("List 1: " + list1); // Output: [1, 2]
ListIn this example, we have a list of fruits and we want to remove all occurrences of the word "banana". We use the `removeAll` method with a singleton collection containing the item we want to remove. The `removed` variable will be true because elements were removed. Finally, we print the modified `list`. The `removeAll` method is part of the `java.util` package, which is included in the standard Java library.list = new ArrayList<>(Arrays.asList("apple", "banana", "kiwi", "mango", "banana")); String item = "banana"; boolean removed = list.removeAll(Collections.singleton(item)); System.out.println("Elements removed: " + removed); System.out.println("List: " + list); // Output: [apple, kiwi, mango]