1. Overview

In this article, We will learn Java remove multiple objects from ArrayList. List.remove(index) or List.remove(Object) can be used to remove single element from ArrayList but what if remove multiple elements from ArrayList collection:

2. Examples

1. Remove multiple objects using List.removeAll method

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

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

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:

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.

Internally, removeIf method is using Iterator

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:

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
Tags: , ,

Leave a Reply

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