Java Vector remove() removeIf() MethodsTutorial

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

What is Java Vector remove() Method?

The Java Vector remove() method is used to remove an element of a vector object at a specific index.

Basically, we put the index number of the element that want to be removed as the argument of this method and it will be removed after that.

Java Vector remove() Method Syntax:

public E remove(int index)

remove() Method Parameters:

The method takes one argument and that is the index number of the element we want to remove from the target vector.

remove() Method Return Value:

The return value of this method is the removed element of the target vector object.

remove() Method Exceptions:

This method might throw one exception and that is:

ArrayIndexOutOfBoundsException: we get this exception if we put an index number that is out of the range of the indexes in the target vector object. For example, if the vector has indexes ranged from 0 to 10 but we set the value 11 instead!

Example: using Vector remove() method

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

    public static void main(String[] args){
        
      Vector <String> vector = new Vector<>();
      vector.add("Jack");
      vector.add("Omid");
      vector.add("Ellen");
      vector.add("John");

      vector.remove(0);
      vector.remove(1);
      for (String s: vector){
         System.out.println(s);
      }
    }
}

Output:

Omid

John

What is Java Vector removeIf() Method?

The Java Vector removeIf() method is used to remove specific elements from a vector object that meet a certain conditions.

Basically, this method takes a reference to a method and that method will be executed for each element of a vector object. Inside this reference method, we define a set of conditions that every element of a vector will be compared with. If the result of this reference method was true, then the target element met the conditions and hence it should be removed from the vector object.

Java Vector removeIf() Method Syntax:

public boolean removeIf(Predicate<? super E> filter)

removeIf() Method Parameters:

The method takes one argument and that is a reference to the functional interface Predicate.

This means the argument of this method is basically a reference to a method that:

  • Takes one argument and that is of the same type as the elements of the target vector object.
  • Its return value is of Boolean type.

Now, in the body of this reference method, we define the conditions.

Note: we can create this method using lambda expression, or referring to an instance or a static method if there’s one with the same signature.

removeIf() Method Return Value:

The return value of this method is of type Boolean.

If any element from the target vector object was removed, the return value of this method becomes true. Otherwise, the value false will return instead.

removeIf() Method Exceptions:

This method might throw two types of exception:

NullPointerException: we get this exception if the argument of this method is set to null.

UnsupportedOperationException: we get this exception if the method is not supported for the target vector object.

Example: using Vector removeIf() method

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

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

      vector.removeIf(e->{
         return e<5? true: false;
      });

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

Output:

5

6

7

8

9

 

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies