Java Vector forEach() Method

The forEach() method of Java Vector class is used to perform the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Actions are performed in the order of iteration if that order is specified.

Syntax:

Following is the declaration of forEach() method:

Parameter:

ParameterDescriptionRequired/Optional
actionIt is an action which will be performed for each element.Required

Returns:

The forEach() method does not return anything. It only performs the given action for each element.

Exceptions:

NullPointerException- This method has thrown an exception if the specified action is null.

Compatibility Version:

Java 1.2 and above

Example 1:

Test it Now

Output:

1
2
3
4
5

Example 2:

Test it Now

Output:

White
Green
Black
Pink

Next TopicJava Vector




Latest Courses