The Wayback Machine - https://web.archive.org/web/20240618020201/https://www.geeksforgeeks.org/vector-iterator-method-in-java-with-examples/
Open In App

Vector iterator() method in Java with Examples

Last Updated : 17 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

 



Previous Article
Next Article

Similar Reads

ArrayList iterator() method in Java with Examples
The iterator() method of ArrayList class in Java Collection Framework is used to get an iterator over the elements in this list in proper sequence. The returned iterator is fail-fast. Syntax: Iterator iterator() Parameter: This method do not accept any parameter. Return Value: This method returns an iterator over the elements in this list in proper
2 min read
AbstractList iterator() method in Java with Examples
The iterator() method of java.util.AbstractList class is used to return an iterator over the elements in this list in proper sequence. This implementation returns a straightforward implementation of the iterator interface, relying on the backing list's size(), get(int), and remove(int) methods. Syntax: public Iterator iterator() Returns Value: This
2 min read
AbsractCollection iterator() Method in Java with Examples
The iterator() method of Java AbstractCollection is used to return an iterator of the same elements as that of the Collection. The elements are returned in random order from what was present in the Collection. Syntax: Iterator iterate_value = AbstractCollection.iterator(); Parameters: The function does not take any parameter. Return Value: The meth
2 min read
DelayQueue iterator() method in Java with Examples
The iterator() method of DelayQueue is used to return an iterator over all the elements in the DelayQueue. These elements can be expired or unexpired.Syntax: public Iterator iterator () Parameters: This method does not accept any parameter.Returns: This method returns an iterator over elements in this delayqueue.Below program to illustrate DelayQue
2 min read
Set iterator() method in Java with Examples
The java.util.Set.iterator() method is used to return an iterator of the same elements as the set. The elements are returned in random order from what present in the set. Syntax: Iterator iterate_value = Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method iterates over the elements of the set and returns t
1 min read
Path iterator() method in Java with Examples
The iterator() method of java.nio.file.Path used to returns an iterator of the name elements which construct this path. The first element of this iterator contains the name element that is closest to the root in the directory hierarchy, the second element is the next closest, and so on. The last element of this iterator is the name of the file or d
2 min read
SortedSet iterator() method in Java with Examples
The java.util.SortedSet.iterator() method is used to return an iterator of the same elements as the set. The elements are returned in random order from what present in the set. Syntax: Iterator iterate_value = sortedSetInstance.iterator(); Parameters: The function does not take any parameter. Return Value: The method iterates over the elements of t
1 min read
BlockingDeque iterator() method in Java with examples
The iterator() method of BlockingDeque returns an iterator over the elements in this deque in a proper sequence. The elements will be returned in order from first (head) to last (tail). The returned iterator is a "weakly consistent" iterator. Syntax: public Iterator iterator() Parameters: This method does not accept any parameter. Returns: This met
2 min read
Copy Elements of One Java Vector to Another Vector in Java
Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector&lt;Integer&gt; gfg=new Vector&lt;&gt;(); Ways To copy elements o
3 min read
Difference between Iterator and Enumeration in Java with Examples
Iterator: It is a universal iterator as we can apply it to any Collection object. By using Iterator, we can perform both read and remove operations. It is an improved version of Enumeration with the additional functionality of remove-ability of an element. Iterator must be used whenever we want to enumerate elements in all Collection framework impl
2 min read
Article Tags :
Practice Tags :