Thursday, December 26, 2024
Google search engine
HomeLanguagesJavaVector iterator() method in Java with Examples

Vector iterator() method in Java with Examples

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

 

RELATED ARTICLES

Most Popular

Recent Comments