Java Vector removeAll() removeAllElements() Methods Tutorial

In this section, we will learn what the Vector removeAll(), removeAllElements() methods are and how to use them in Java.

What is Java Vector removeAll() Method?

The Java Vector removeAll() method is used to remove those elements of a vector object that are also in another collection! We pass a reference of this collection as the argument to the method.

Java Vector removeAll() Method Syntax:

public boolean removeAll(Collection<?> c)

removeAll() Method Parameters:

The method takes one argument and that is a reference to the collection that wants to compare its elements with the elements of the target vector object.

removeAll() Method Return Value:

The return value of this method is of Boolean type.

If one or more elements of the target vector object got removed as a result of calling this method, the return value becomes true. Otherwise, the value false will return instead.

removeAll() Method Exceptions:

ClassCastException: We get this exception if the elements of the reference collection (the argument) is of incompatible datatype compared to the elements of the target vector object.

NullPointerException: We get this exception if we pass a null value to this method.

Example: using Vector removeAll() method

import java.util.Vector; 
import java.util.ArrayList;
public class Main {

    public static void main(String[] args){
      Vector<Integer> list = new Vector<>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      list.add(6);
      list.add(7);

      ArrayList<Integer> array = new ArrayList<>();
      array.add(1);
      array.add(2);
      array.add(3);

      list.removeAll(array);

      for (int i: list){
         System.out.println(i);
      }       
    }
}

Output:

4

5

6

7

What is Java Vector removeAllElements() Method?

The Java Vector removeAllElements() method is used to remove the entire elements of a vector object and as a result, set its size to 0.

Java Vector removeAllElements() Method Syntax:

public void removeAllElements()

removeAllElements() Method Parameters:

The method does not take an argument.

removeAllElements() Method Return Value:

The return value of this method is void.

removeAllElements() Method Exceptions:

The method does not throw an exception.

Example: using Vector removeAllElements() method

import java.util.Vector; 

public class Main {

    public static void main(String[] args){
      Vector<Integer> list = new Vector<>();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      list.add(6);
      list.add(7);

      System.out.println("The size of the vector object before callng the removeAllElements method: "+list.size());
      list.removeAllElements();
      System.out.println("The size of the vector object after calling the removeAllElements method: "+ list.size());      
    }
}

Output:

The size of the vector object before calling the removeAllElements method: 7

The size of the vector object after calling the removeAllElements method: 0
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies