Vector forEach() method in Java

The forEach() method of Vector is used to perform a given action for every element of the Iterable of Vector until all elements have been Processed by the method or an exception occurs.

The operations are performed in the order of iteration if the order is specified by the method. Exceptions thrown by the Operation are passed to the caller.

Until and unless an overriding class has specified a concurrent modification policy, the operation cannot modify the underlying source of elements so we can say that behavior of this method is unspecified.

Retrieving Elements from Collection in Java.

Syntax:

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

Parameter: This method takes a parameter action which represents the action to be performed for each element.

Return Value: This method does not returns anything.

Exception: This method throws NullPointerException if the specified action is null.

Below programs illustrate forEach() method of Vector:

Example 1: Program to demonstrate forEach() method on Vector which contains a collection of String.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate forEach()
// method of Vector
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
        // create an Vector which going to
        // contains a collection of Strings
        Vector<String> data = new Vector<String>();
  
        // Add String to Vector
        data.add("Saltlake");
        data.add("LakeTown");
        data.add("Kestopur");
  
        System.out.println("List of Strings data");
        // forEach method of Vector and
        // print data
        data.forEach((n) -> System.out.println(n));
    }
}

chevron_right


Output:

List of Strings data
Saltlake
LakeTown
Kestopur

Example 2: Program to demonstrate forEach() method on Vector which contains collection of Objects.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate forEach()
// method of Vector
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
        // create an Vector which going to
        // contains a collection of objects
        Vector<DataClass> vector = new Vector<DataClass>();
  
        // Add objects to vector
        vector.add(new DataClass("Shape", "Square"));
        vector.add(new DataClass("Area", "2433Sqft"));
        vector.add(new DataClass("Radius", "25m"));
  
        // print result
        System.out.println("list of Objects:");
  
        // forEach method of Vector and
        // print Objects
        vector.forEach((n) -> print(n));
    }
  
    // printing object data
    public static void print(DataClass n)
    {
        System.out.println("****************");
        System.out.println("Object Details");
        System.out.println("key : " + n.key);
        System.out.println("value : " + n.value);
    }
}
  
// create a class
class DataClass {
  
    public String key;
    public String value;
  
    DataClass(String key, String value)
    {
        this.key = key;
        this.value = value;
    }
}

chevron_right


Output:

list of Objects:
****************
Object Details
key : Shape
value : Square
****************
Object Details
key : Area
value : 2433Sqft
****************
Object Details
key : Radius
value : 25m

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/Vector.html#forEach(java.util.function.Consumer)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.