Java ArrayList.removeAll() - Syntax & Examples

[Pages:4]Java ArrayList.removeAll() ? Syntax & Examples

Java ArrayList.removeAll() ? Examples

In this tutorial, we will learn about the Java ArrayList.removeAll() method, and learn how to use this method to remove the elements that match with the any of the elements in the given collection, with the help of examples.

removeAll(Collection)

ArrayList.removeAll() removes from this list all of its elements that are contained in the specified collection.

Syntax

The syntax of removeAll() method is removeAll(Collection collection)

where

Parameter Description

collection The collection containing elements to be removed from this ArrayList. Returns The method returns boolean value.

Example 1 ? removeAll(collection)

In this example, we will take an ArrayList of strings with the elements: "a", "b", "c", "d", "e", "f" . We will also take a collection "d", "e", "m" . We will remove the elements of this ArrayList which match any of the element in the collection using ArrayList.removeAll() method.

Elements "d" and "e" from collection match some of the elements in ArrayList. So, these elements will be removed from the ArrayList.

Element "m" from collection does not match any element from the ArrayList . Nothing happens for this element.

Java Program

import java.util.ArrayList; import java.util.Arrays; import java.util.List;

public class Example { public static void main(String[] args) { ArrayList arrayList = new ArrayList( Arrays.asList("a", "b", "c", "d", "e", "f")); List collection = Arrays.asList("d", "e", "m");

System.out.println("Original ArrayList

: " + arrayList);

arrayList.removeAll(collection);

System.out.println("ArrayList after removeAll() : " + arrayList);

}

}

Output

Original ArrayList

: [a, b, c, d, e, f]

ArrayList after removeAll() : [a, b, c, f]

Conclusion

In this Java Tutorial, we have learnt the syntax of Java ArrayList.removeAll() method, and also learnt how to use this method with the help of Java example programs.

Java ArrayList Java ArrayList Java ArrayList - add() Java ArrayList - addAll() Java ArrayList - clear() Java ArrayList - clone() Java ArrayList - contains() Java ArrayList - ensureCapacity()

Java ArrayList - forEach() Java ArrayList - get() Java ArrayList - indexOf() Java ArrayList - isEmpty() Java ArrayList - iterator() Java ArrayList - lastIndexOf() Java ArrayList - listIterator() Java ArrayList - remove() Java ArrayList - removeAll() Java ArrayList - removeIf() Java ArrayList - removeRange() Java ArrayList - retainAll() Java ArrayList - set() Java ArrayList - size() Java ArrayList - spliterator() Java ArrayList - subList() Java ArrayList - toArray() Java ArrayList - trimToSize()

Java Tutorial Java Tutorial Java Introduction Java Installation IDEs for Java Development Java Datatypes Java Operators Java Decision Making Java Loops Java Exception Handling Java File Operations Java OOPs Java Array Java String

Java String Java Date & Time Java MySQL Java Random Java Math Java ArrayList

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download