79
Java ArrayList retainAll() method
The retainAll () method of Java ArrayList class keeps only elements in the original list that are contained in the specified collection. In other words, it replaces the original list with the specified list.
Syntax:
Parameter:
“c”: is the Collection that contains elements to be retained in this list.
Return:
Return “true”: if the collection “c” has been retained successfully in the original list.
Exception:
java.lang.ClassCastException: if the class of an element of this list is incompatible with the specified collection.
java.lang.NullPointerException: If the specified collection “c” contains null element and the specified collection does not permit null elements, or the specified collection is null.
Example 1
Output:
[10] [1, 2, 3] true [10]
Example 2
Output:
null [1, 2]
Example 3
Output:
[2, null] [1, 3] true []
Next TopicJava ArrayList