Vector iterator() method in Java with Examples

17 Dec, 2021
iterator() method of Vector class that is present inside java.util package is used to return an iterator of the same elements as that of the Vector. The elements are returned in random order from what was present in the vector.
Syntax: 
Iterator iterate_value = Vector.iterator();
Parameters: The function does not take any parameter.
Return Type: The method iterates over the elements of the vector and returns the values(iterators).
Example 1:
Java
// Java code to illustrate iterator() Method// of Vector class // Importing required classesimport java.util.*;import java.util.Vector; // Main classpublic class GFG {     // Main driver method    public static void main(String args[])    {        // Creating an empty Vector of string type        Vector<String> vector = new Vector<String>();         // Adding elements into the Vector        // using add() method        vector.add("Welcome");        vector.add("To");        vector.add("Geeks");        vector.add("4");        vector.add("Geeks");         // Printing and displaying the Vector        System.out.println("Vector: " + vector);         // Now creating an iterator        Iterator value = vector.iterator();         // Display message only        System.out.println("The iterator values are: ");         // Condition holds true till there is single element        // remaining using hasNext() method        while (value.hasNext()) {             // Displaying the values            // after iterating through the vector            System.out.println(value.next());        }    }}
Vector: [Welcome, To, Geeks, 4, Geeks]
The iterator values are: 
Welcome
To
Geeks
4
Geeks
 
Example 2:
Java
// Java code to illustrate iterator() method usage// with usage of hashCode() method // Importing required classesimport java.util.*; // Main classpublic class GFG {     // Main driver method    public static void main(String args[])    {        // Creating an empty Vector of integer type        Vector<Integer> vector = new Vector<Integer>();         // Adding custom elements into the Vector        // using add() method        vector.add(10);        vector.add(20);        vector.add(30);        vector.add(40);        vector.add(50);         // Displaying the Vector        System.out.println("Vector: " + vector);         // Creating an iterator        Iterator value = vector.iterator();         // Display message for better readability        System.out.println("The iterator values are: ");         // Holds true till there is single element        while (value.hasNext()) {             // Printing the values after iterating            // through the vector            // using next() method            System.out.println(value.next());        }    }}
Vector: [10, 20, 30, 40, 50]
The iterator values are: 
10
20
30
40
50
 
solankimayank
simranarora5sos
Java - util package
Java-Collections
Java-Functions
Java-Vector
Picked
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Arrays in Java
Arrays.sort() in Java with examples
Split() String method in Java with examples
Reverse a string in Java
Object Oriented Programming (OOPs) Concept in Java
For-each loop in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java