Java ArrayList retainAll() method21 Mar 2025 | 1 min read 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 1Output: [10] [1, 2, 3] true [10] Example 2Output: null [1, 2] Example 3Output: [2, null] [1, 3] true [] Next TopicJava ArrayList |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India