Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the Vector.retainAll method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Vector class is a growable array of objects. The elements of Vector can be accessed using an integer index and the size of a Vector can be increased or decreased. Read more about Vector here.

The retainAll method removes all the elements of the Vector object which are not present in the passed collection.

Syntax

boolean retainAll(Collection<?> c)

Parameters

This method takes the collection object to be retained in this Vector object as an argument.

Return value

This method returns true if the Vector changes as a result of the call (any element is removed from the Vector due to the element not being present in the collection). Otherwise, false is returned.

Code

import java.util.Vector;
import java.util.ArrayList;
class RetainAll {
public static void main( String args[] ) {
Vector<Integer> vector = new Vector<>();
vector.add(1);
vector.add(2);
vector.add(3);
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(4);
System.out.println("The vector is "+ vector);
System.out.println("The list is "+ list);
System.out.println("\nCalling vector.retainAll(list). Is vector changed - " + vector.retainAll(list));
System.out.println("\nThe vector is "+ vector);
}
}

In the code above,

In line numbers 1 and 2: We import the Vector and ArrayList classes.

From line number 5 to 8: We create a new Vector object with the name vector and add three elements (1,2,3) to the vector object using the add method.

From line numbers 10 to 12: We create a new ArrayList object with the name list and add two elements (1,4) to the list object using the add method.

In line number 16: We use the retainAll method to retain only elements which are present in the list. In our case, the elements of the list and vector are listed below:

Elements of list -- 1,4
Elements of vector -- 1,2,3

The retainAll will,

  • Check if element 1 of vector is present in the list. The list contains 1. So the 1 is retained in the vector.

  • Check if element 2 of vector is present in the list. The list doesn’t contain 2. So the 2 is removed from the vector.

  • Check if element 3 of vector is present in the list. The list doesn’t contain 3. So the 3 is removed from the vector.

The resulting vector will be [1].

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring