Java Utility Library

Java Vector - replaceAll() Method



The java.util.Vector.replaceAll() method is used to replace each element of the list with the result of applying the specified unary operator to that element.

Syntax

public void replaceAll(UnaryOperator<E> operator)

Parameters

operator Specify the unary operator to apply to each element.

Return Value

void type.

Exception

NA.

Example:

In the example below, the java.util.Vector.replaceAll() method is used to replace each element of the vector by using specified unary operator.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> vec = new Vector<Integer>();

    //populating vec
    vec.add(10);
    vec.add(20);
    vec.add(30);
    vec.add(40);
    vec.add(50);

    //printing vec
    System.out.println("vec contains: " + vec);

    //applying ++ operator
    vec.replaceAll(n -> ++n);

    //printing vec
    System.out.println("vec contains: " + vec);

    //applying += operator
    vec.replaceAll(n -> n+=5);
 
    //printing vec
    System.out.println("vec contains: " + vec);
  }
}

The output of the above code will be:

vec contains: [10, 20, 30, 40, 50]
vec contains: [11, 21, 31, 41, 51]
vec contains: [16, 26, 36, 46, 56]

❮ Java.util - Vector