Skip to content
Related Articles

Related Articles

Vector iterator() method in Java with Examples

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 17 Dec, 2021
View Discussion
Improve Article
Save Article

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 classes
import java.util.*;
import java.util.Vector;
 
// Main class
public 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());
        }
    }
}

Output: 

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 classes
import java.util.*;
 
// Main class
public 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());
        }
    }
}

Output: 

Vector: [10, 20, 30, 40, 50]
The iterator values are: 
10
20
30
40
50

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!