The Wayback Machine - https://web.archive.org/web/20240618020221/https://www.geeksforgeeks.org/vector-foreach-method-in-java/
Open In App

Vector forEach() method in Java

Last Updated : 17 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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.




// 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));
    }
}


Output:

List of Strings data
Saltlake
LakeTown
Kestopur

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




// 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;
    }
}


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)



Previous Article
Next Article

Similar Reads

foreach() loop vs Stream foreach() vs Parallel Stream foreach()
foreach() loopLambda operator is not used: foreach loop in Java doesn't use any lambda operations and thus operations can be applied on any value outside of the list that we are using for iteration in the foreach loop. The foreach loop is concerned over iterating the collection or array by storing each element of the list on a local variable and do
4 min read
Difference Between Collection.stream().forEach() and Collection.forEach() in Java
Collection.forEach() and Collection.stream().forEach() are used for iterating over the collections, there is no such major difference between the two, as both of them give the same result, though there are some differences in their internal working. Collection.stream().forEach() is basically used for iteration in a group of objects by converting a
3 min read
Copy Elements of One Java Vector to Another Vector in Java
Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector&lt;Integer&gt; gfg=new Vector&lt;&gt;(); Ways To copy elements o
3 min read
Stream forEach() method in Java with examples
Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : void forEach(Consumer&lt;? super T&gt; action) Where, Consumer is a functional interface and T is the type of stream elements. Note
2 min read
IntStream forEach() method in Java
IntStream forEach(IntConsumer action) performs an action for each element of the stream. IntStream forEach(IntConsumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : void forEach(IntConsumer action) Parameter : IntConsumer represents an operation that accepts a single int-valued argume
2 min read
Iterable forEach() method in Java with Examples
It has been Quite a while since Java 8 released. With the release, they have improved some of the existing APIs and added few new features. One of them is forEach Method in java.lang.Iterable Interface. Whenever we need to traverse over a collection we have to create an Iterator to iterate over the collection and then we can have our business logic
3 min read
ArrayList forEach() method in Java
The forEach() method of ArrayList used to perform the certain operation for each element in ArrayList. This method traverses each element of the Iterable of ArrayList until all elements have been Processed by the method or an exception is raised. The operation is performed in the order of iteration if that order is specified by the method. Exceptio
2 min read
HashTable forEach() method in Java with Examples
The forEach(BiConsumer) method of Hashtable class perform the BiConsumer operation on each entry of hashtable until all entries have been processed or the action throws an exception. The BiConsumer operation is a function operation of key-value pair of hashtable performed in the order of iteration. Method traverses each element of Hashtable until a
2 min read
LinkedTransferQueue forEach() method in Java with Examples
The forEach() method of Java.util.concurrent.LinkedTransferQueue is an in-built function in Java which is used to traverse each element in this queue. Syntax: public void forEach(Consumer&lt;E&gt; action) Parameters: This method takes a parameter action which represents the action to be performed for each element. Return Value: This method does not
2 min read
LinkedBlockingDeque forEach() method in Java with Examples
The forEach() method of LinkedBlockingDeque performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Syntax: public void forEach(Consumer&lt;E&gt; action) Parameters: This method takes a parameter action which represents the action to be performed for each element. Return V
2 min read