|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "ArrayList retainAll() method" |
| 4 | +author: gaurav |
| 5 | +categories: [Collections, ArrayList] |
| 6 | +description: "Let's see the retainAll() method of the Arraylist class in Java." |
| 7 | +--- |
| 8 | + |
| 9 | +Let's see the `retainAll()` method of the [ArrayList class in Java](https://coderolls.com/arraylist-in-java/). |
| 10 | + |
| 11 | +This method is used to retains all the elements in of the collection in the list. |
| 12 | + |
| 13 | +## Introduction |
| 14 | +ArrayList is a widely used class to store and retrieve data in a collection framework. |
| 15 | + |
| 16 | +ArrayList is an ordered collection i.e. it maintains the insertion order of the elements. Also, it allows duplicate elements in the list. |
| 17 | + |
| 18 | +Also, We have seen a few methods of the ArrayList class before like [`add()`](https://coderolls.com/add-element-in-arraylist/), [`addAll()`](http://https://coderolls.com/arraylist-addall-method-in-java/), [`remove()`](https://coderolls.com/remove-element-from-arraylist/), [`set()`](https://coderolls.com/change-element-in-arraylist/), [`iterate()`](https://coderolls.com/iterating-the-arraylist-in-java/), [`clear()`](https://coderolls.com/arraylist-clear-method-in-java/), [`contains()`](https://coderolls.com/arraylist-contains-method), [`get()`](https://coderolls.com/arraylist-get-method) and [`removeRange()` ](https://coderolls.com/arraylist-removerange-method) method. |
| 19 | + |
| 20 | +Today we will see the `retainAll()` method. |
| 21 | + |
| 22 | + |
| 23 | +## ArrayList `retainAll(Collection c)` method |
| 24 | + |
| 25 | +The `retainAll()` method retains all the elements of the collections passed as a parameter in the list. |
| 26 | + |
| 27 | +That means it will remove all elements from the list other than the ones which are present in the collection that is passed as parameter. |
| 28 | + |
| 29 | +The method is signature is given below. |
| 30 | + |
| 31 | +```java |
| 32 | +public boolean retainAll(Collection<?> c) |
| 33 | +``` |
| 34 | + |
| 35 | +As shown in the method signature, it accepts only one parameter i.e. `Collection c`. It is a collection of the objects that we have to retain in the list. |
| 36 | + |
| 37 | +Also, the `retainAll()` method has a return type as a `boolean`. It returns `true` only if the list is changed as a result of the method call otherwise `false`. |
| 38 | + |
| 39 | +Let's see the java program for better understanding. |
| 40 | + |
| 41 | +```java |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.List; |
| 44 | + |
| 45 | +/** |
| 46 | + * A java program to to eplain the retainAll() method of the Arraylist class. |
| 47 | + * |
| 48 | + * @author Gaurav Kukade at coderolls.com |
| 49 | + * |
| 50 | + */ |
| 51 | +public class ArrayListRetainAllExample { |
| 52 | + |
| 53 | + public static void main(String[] args) { |
| 54 | + |
| 55 | + //create an empty arraylist of Integer to retain elements |
| 56 | + List<Integer> numbers1 = new ArrayList<Integer>(); |
| 57 | + |
| 58 | + //add number to the list |
| 59 | + numbers1.add(4); |
| 60 | + numbers1.add(5); |
| 61 | + numbers1.add(6); |
| 62 | + |
| 63 | + //create an empty arraylist of Integer |
| 64 | + List<Integer> numbers2 = new ArrayList<Integer>(); |
| 65 | + |
| 66 | + // add number to the list |
| 67 | + numbers2.add(1); |
| 68 | + numbers2.add(2); |
| 69 | + numbers2.add(3); |
| 70 | + numbers2.add(4); |
| 71 | + numbers2.add(5); |
| 72 | + numbers2.add(6); |
| 73 | + numbers2.add(7); |
| 74 | + numbers2.add(8); |
| 75 | + numbers2.add(9); |
| 76 | + |
| 77 | + System.out.println("Lists before the method call"); |
| 78 | + System.out.println("numbers1: "+ numbers1); |
| 79 | + System.out.println("numbers2: "+ numbers2); |
| 80 | + |
| 81 | + //retain all the elements of numbers1 in the list numbers2 |
| 82 | + numbers2.retainAll(numbers1); |
| 83 | + |
| 84 | + System.out.println("\nLists after the method call"); |
| 85 | + System.out.println("numbers1: "+ numbers1); |
| 86 | + System.out.println("numbers2: "+ numbers2); |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +Output: |
| 92 | + |
| 93 | +``` |
| 94 | +Lists before the method call |
| 95 | +numbers1: [4, 5, 6] |
| 96 | +numbers2: [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 97 | +
|
| 98 | +Lists after the method call |
| 99 | +numbers1: [4, 5, 6] |
| 100 | +numbers2: [4, 5, 6] |
| 101 | +``` |
| 102 | +### Exceptions thrown by `retainAll()` Methods |
| 103 | +This method may throw the `ClassCastException` and `NullPointerException`. |
| 104 | + |
| 105 | +#### `ClassCastException` |
| 106 | +This method may throw the `ClassCastException`, if the class of an element of this list is incompatible with the specified collection. (optional) |
| 107 | + |
| 108 | +#### `NullPointerException` |
| 109 | +This method may throw the `NullPointerException`, if this list contains a null element and the specified collection does not permit null elements (optional) or if the specified collection is null. |
| 110 | + |
| 111 | +The java example for the `NullPointerException` is given below. |
| 112 | + |
| 113 | +```java |
| 114 | +import java.util.ArrayList; |
| 115 | +import java.util.List; |
| 116 | +/** |
| 117 | + * A java program to show the NullPointerException in retainAll() method |
| 118 | + * of the arrayList class. |
| 119 | + * |
| 120 | + * @author gaurav |
| 121 | + * |
| 122 | + */ |
| 123 | +public class RetainAllNullPointerExceptionExample { |
| 124 | + |
| 125 | + public static void main(String[] args) { |
| 126 | + |
| 127 | + //create an empty arraylist of Integer to retain elements |
| 128 | + List<String> numbers1 = null; |
| 129 | + |
| 130 | + //create an empty arraylist of Integer |
| 131 | + List<Integer> numbers2 = new ArrayList<Integer>(); |
| 132 | + |
| 133 | + // add number to the list |
| 134 | + numbers2.add(1); |
| 135 | + numbers2.add(2); |
| 136 | + numbers2.add(3); |
| 137 | + numbers2.add(4); |
| 138 | + |
| 139 | + System.out.println("Lists before the method call"); |
| 140 | + System.out.println("numbers1: "+ numbers1); |
| 141 | + System.out.println("numbers2: "+ numbers2); |
| 142 | + |
| 143 | + //retain all the elements of numbers1 in the list numbers2 |
| 144 | + numbers2.retainAll(numbers1); |
| 145 | + |
| 146 | + System.out.println("\nLists after the method call"); |
| 147 | + System.out.println("numbers1: "+ numbers1); |
| 148 | + System.out.println("numbers2: "+ numbers2); |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +Output: |
| 154 | +``` |
| 155 | +Lists before the method call |
| 156 | +numbers1: null |
| 157 | +numbers2: [1, 2, 3, 4] |
| 158 | +Exception in thread "main" java.lang.NullPointerException |
| 159 | + at java.util.Objects.requireNonNull(Objects.java:203) |
| 160 | + at java.util.ArrayList.retainAll(ArrayList.java:714) |
| 161 | + at com.gaurav.ExProject.ArrayList.RetainAllClassCastExceptionExample.main(RetainAllClassCastExceptionExample.java:27) |
| 162 | +``` |
| 163 | +## Conclusion |
| 164 | + |
| 165 | +The `retainAll(Collection c)` method retains all the elements of the collection passed as a parameter in the list. |
| 166 | + |
| 167 | +```java |
| 168 | +public boolean retainAll(Collection<?> c) |
| 169 | +``` |
| 170 | + |
| 171 | +This method return `true` if the list is changed otherwise `false`. |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +The example java program used in the above article can be found at [this GitHub repository](https://github.com/coderolls/blogpost-coding-examples/tree/main/collections/arraylist/arraylist-retainall-method). |
| 176 | + |
| 177 | +Please write your thoughts in the comment section below. |
0 commit comments