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.
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.
List of Strings data Saltlake LakeTown Kestopur
Example 2: Program to demonstrate forEach() method on Vector which contains collection of Objects.
list of Objects: **************** Object Details key : Shape value : Square **************** Object Details key : Area value : 2433Sqft **************** Object Details key : Radius value : 25m
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.