Java Vector forEach() Method



Description

The Java Vector forEach(E action) is used to performs a given action for each element of the Iterable until all elements are processed exception occurs during action. In case, an order is specified then actions are performed in the given order. In case of exception, the exception is relayed to the caller.

Declaration

Following is the declaration for java.util.Vector.forEach() method

public void forEach​(Consumer<? super E>> action)

Parameters

action − The action to be performed for each element.

Exception

NullPointerException − if the specified action is null.

Iterating elements of a Vector of Integer using forEach Example

The following example shows the usage of Java Vector forEach(action) method to iterate and print Integers. We're adding couple of Integers to the Vector object using add() method calls per element and then print each element using forEach() to show the elements added.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty vector
      Vector<Integer> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add(1);
      vector.add(2);
      vector.add(3);
      vector.add(4);
      vector.add(5);

      // let us print all the elements available in vector
      vector.forEach(s -> { System.out.println(s);});
   }
}

Output

Let us compile and run the above program, this will produce the following result −

1
2
3
4
5

Iterating elements of a Vector of String using forEach Example

The following example shows the usage of Java Vector forEach(action) method to iterate and print Strings. We're adding couple of Strings to the Vector object using add() method calls per element and then print each element using forEach() to show the elements added.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty vector
      Vector<String> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add("A");
      vector.add("B");
      vector.add("C");
      vector.add("D");
      vector.add("E");

      // let us print all the elements available in vector
      vector.forEach(s -> { System.out.println(s);});
   }
}

Output

Let us compile and run the above program, this will produce the following result −

A
B
C
D
E

Iterating elements of a Vector of Student objects using forEach Example

The following example shows the usage of Java Vector forEach(action) method to iterate and print Student objects. We're adding couple of Student objects to the Vector object using add() method calls per element and then print each element using forEach() to show the elements added.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty vector
      Vector<Student> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add(new Student(1, "Julie"));
      vector.add(new Student(2, "Robert"));
      vector.add(new Student(3, "Adam"));

      // let us print all the elements available in vector
      vector.forEach(s -> { System.out.println(s);});
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

[ 1, Julie ]
[ 2, Robert ]
[ 3, Adam ]
java_util_vector.htm
Advertisements