Vector forEach() method in Java Last Updated : 17 Sep, 2018 Comments Improve Suggest changes Like Article Like 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 // 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 // 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) Comment More infoAdvertise with us Next Article Vector forEach() method in Java A AmanSingh2210 Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-Vector +1 More Practice Tags : JavaJava-Collections Similar Reads Vector addAll() Method in Java In Java, the addAll() method is used to add all elements from a specified Collection to the current Vector. The addAll() method allows you to efficiently append multiple elements to a vector in one operation.Implementation:Java// Java Program to addAll elements // From Vector to Vector import java.u 3 min read Vector addElement() Method in Java The Java.util.Vector.addElement() method is used to append a specified element to the end of this vector by increasing the size of the vector by 1. The functionality of this method is similar to that of the add() method of Vector class.Syntax: boolean addElement(Object element)Parameters: This funct 2 min read Vector capacity() Method in Java The Java.util.Vector.capacity() method in Java is used to get the capacity of the Vector or the length of the array present in the Vector.Syntax of Vector capacity() methodVector.capacity()Parameters: The method does not take any parameter. Return Value: The method returns the capacity or the intern 2 min read Vector clear() Method in Java The Java.util.Vector.clear() method is used to remove all the elements from a Vector. Using the clear() method only clears all the element from the vector and does not delete the vector. In other words, we can say that the clear() method is used to only empty an existing vector. Syntax:Vector.clear( 2 min read Java Vector clone() Method In Java, the clone() method of the Vector class is used to return a shallow copy of the vector. It just creates a copy of the vector. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array.Example 1: In this example, we use the cl 3 min read Java Vector contains() Method The contains() method in Java is used to check whether a specific element is present in the Vector or not.Example 1: In this example, we will check whether a particular string is present in the vector or not.Java// Java program to demonstrate the use // of the contains() method with Strings import j 3 min read Java Vector containsAll() Method In Java, the containsAll() method is used to check if the Vector contains all the elements of a specified Collection or not.Example 1: Below program demonstrating the use of containsAll() method with Strings.Java// Java Program to demonstrate the use of // containsAll() with Strings import java.util 2 min read Java Vector copyInto() Method In Java, the copyInto() method is used to copy all the components from one vector to another array, having enough space to hold all of the components of the vector. It is to be noted that the index of the elements remains unchanged. The elements present in the array are replaced by the elements of t 3 min read Java Vector elementAt() Method In Java, the elementAt() method is used to fetch or retrieve an element at a specific index from a Vector. It allows to access elements in the vector by providing the index, which is zero-based.Example 1: Here, we use the elementAt() method to retrieve an element at a specified index with an Integer 2 min read Java Vector elements() Method In Java, the elements() method is used to return an enumeration of the elements in the Vector. This method allows you to iterate over the elements of a Vector using an Enumeration, which provides methods for checking if there are more elements and retrieving the next element.Example 1: Below is the 2 min read Like