List retainAll() Method in Java with Examples Last Updated : 02 Jan, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report This method is used to retain all the elements present in the collection from the specified collection into the list. Syntax: boolean retainAll(Collection c) Parameters: This method has only argument, collection of which elements are to be retained in the given list. Returns: This method returns True if elements are retained and list changes. Below programs show the implementation of this method. Program 1: Java // Java code to show the implementation of // retainAll method in list interface import java.util.*; public class GfG { // Driver code public static void main(String[] args) { // Initializing a list of type Linkedlist List<Integer> l = new LinkedList<>(); l.add(1); l.add(3); l.add(5); l.add(7); l.add(9); System.out.println(l); ArrayList<Integer> arr = new ArrayList<>(); arr.add(3); arr.add(5); l.retainAll(arr); System.out.println(l); } } Output: [1, 3, 5, 7, 9] [3, 5] Program 2: Below is the code to show implementation of list.retainAll() using Linkedlist. Java // Java code to show the implementation of // retainAll method in list interface import java.util.*; public class GfG { // Driver code public static void main(String[] args) { // Initializing a list of type Linkedlist List<String> l = new LinkedList<>(); l.add("10"); l.add("30"); l.add("50"); l.add("70"); l.add("90"); System.out.println(l); ArrayList<String> arr = new ArrayList<>(); arr.add("30"); arr.add("50"); l.retainAll(arr); System.out.println(l); } } Output: [10, 30, 50, 70, 90] [30, 50] Reference: Oracle Docs Comment More infoAdvertise with us Next Article Vector retainAll() method in Java with Examples B barykrg Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions java-list +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads List removeAll() method in Java with Examples This method is used to remove all the elements present in the collection from the specified list. Syntax: boolean removeAll(Collection c) Parameters: This method has only argument, collection of which elements are to be removed from the given list. Returns: This method returns True if elements are r 2 min read Vector retainAll() method in Java with Examples The retainAll method of class vector in java is an inbuilt function in Java which is used to retains only the elements in this Vector that are contained in the specified Collection. In other words, removes from this Vector all of its elements that are not contained in the specified Collection. Synta 3 min read List size() method in Java with Examples The size() method of the List interface in Java is used to get the number of elements in this list. That is, the list size() method returns the count of elements present in this list container.Example:Java// Java Program to demonstrate // List size() Method import java.util.*; class GFG { public sta 2 min read HashSet retainAll() method in Java with Example The retainAll() method of java.util.HashSet class is used to retain from this set all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this 3 min read Map putAll() Method in Java with Examples This method is used to copy all of the mappings from the specified map to this map. Syntax: void putAll(Map m) Parameters: This method has the only argument map m, which contains key-value mappings to be copied to given map. Returns: This method returns previous value associated with the key if pres 2 min read List sublist() Method in Java with Examples This method gives a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. Syntax: List subList(int fromIndex, int toIndex) Parameters: This function has two parameter fromIndex and toIndex, which are the starting and ending ranges respectively to create 2 min read Like