1. Overview

In this article, We will learn Java remove multiple objects from ArrayList.  Java collection is one of the handy features that attract developers. Collection framework makes developers life easy in terms of storing, searching, removing of any types of data.

Consider a scenario where we want to remove all the elements from the list which satisfy a certain condition or we want to remove all the elements from the large list which subset already exists in another list or even sometimes we want to remove all the elements from a list and insert new elements for some other operations.  Java List provides all the answers to these type of questions by providing facility to remove multiple objects from the ArrayList.

As we know List.remove(index) and List.remove(Object) is used to remove a single element from ArrayList.

Let’s discuss some of the examples of remove multiple objects from ArrayList.

2. Examples

1. Remove multiple objects using List.removeAll method

If both are collection objects and we want to remove all element from another collection then removeAll can be used.

the removeAll method returns true if it successfully removes elements from it otherwise false.

removeAll method throws three types of exceptions such as NullPointerException, ClassCastException, and UnsupportedOperationException

if we are using removeAll and passing collection of custom objects like Employee or Student etc, In that case equals and hashCode require to Override.

In below example, we have created two array lists, In the first list inserted four elements and in the second list inserted two elements. after that, we have called removeAll elements of the first array list and pass the second array list as an argument. By doing this operation from the first ArrayList all the elements removed which exist in the second element.

public class ArrayListMultipleRemoveDemo {
    public static void main(String ... args){
        java.util.List<String> list = new ArrayList<>();  creating ArrayList 
        list.add("apple");
        list.add("banana");
        list.add("mango");
        list.add("barry");

        java.util.List<String> needToRemove = new ArrayList<>();
        needToRemove.add("apple");
        needToRemove.add("banana");

        list.removeAll(needToRemove);        // removeAll will remove multiple elements, We can pass any collection like Set, List or any other

        System.out.println("After Removed From List:");
        list.forEach(System.out::println);
    }
}

Output:

Here as you can see apple and banana elements removed from the first list.

After Removed From List:
mango
barry

2. Remove multiple objects using List.removeIf (Java 8)

Remove multiple elements based on some conditions then we can use removeIf method or Iterator.

removeIf method takes Predicate as an argument and returns true if an element were removed otherwise false.

Error or RuntimeException will be thorw during iteration or Predicate are relayed to the caller.

Internally, removeIf method is using Iterator

In below example, We want to remove elements which startsWith “b”. Here we write inline predicate as a method argument.

public class ArrayListMultipleRemoveDemo {
    public static void main(String ... args){
        java.util.List<String> list = new ArrayList<>();  creating ArrayList 
        list.add("apple");
        list.add("banana");
        list.add("mango");
        list.add("barry");

        list.removeIf(e->e.startsWith("b"));        // removeIf method available in Java 8 only, removeIf return type is boolean, If condition is true then it will remove element from List 

        // After remove element start from b
        list.forEach(System.out::println);
    }
}

Output:

As you can see elements which are starting from string “b” was removed.

apple
mango

3. Remove multiple objects using Iterator (Java 7)

Remove elements using Iterator. While list.remove(index) can be used while iterating elements.

public class ArrayListMultipleRemoveDemo {
    public static void main(String ... args){
        java.util.List<String> list = new ArrayList<>();  creating ArrayList 
        list.add("apple");
        list.add("banana");
        list.add("mango");
        list.add("barry");

        Iterator<String> iterator = list.iterator();      // it will return iterator
        while (iterator.hasNext()){
            String name = iterator.next();
            if(name.startsWith("b")){   
                iterator.remove();                  // remove element if match condition
            }
        }

        for(String name:list){                      // print all remaining elements
            System.out.println(name);
        }
    }
}

Output:

apple
mango

References:

 

 

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *