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

Set iterator() method in Java with Examples

Last Updated : 31 Dec, 2018
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 the values(iterators).

Below program illustrate the java.util.Set.iterator() method:




// Java code to illustrate iterator()
  
import java.util.*;
  
public class SetDemo {
    public static void main(String args[])
    {
        // Creating an empty Set
        Set<String> set = new HashSet<String>();
  
        // Use add() method to add elements into the Set
        set.add("Welcome");
        set.add("To");
        set.add("Geeks");
        set.add("4");
        set.add("Geeks");
  
        // Displaying the Set
        System.out.println("Set: " + set);
  
        // Creating an iterator
        Iterator value = set.iterator();
  
        // Displaying the values after iterating through the iterator
        System.out.println("The iterator values are: ");
        while (value.hasNext()) {
            System.out.println(value.next());
        }
    }
}


Output:

Set: [4, Geeks, Welcome, To]
The iterator values are: 
4
Geeks
Welcome
To

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#iterator()


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
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
2 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
Difference between Iterator and Enumeration in Java with Examples
When working with Java Collections, it's essential to understand the differences between Iterator and Enumeration. Both are used for traversing elements within a collection, but they serve different purposes and have varying capabilities. IteratorAn Iterator is a versatile tool in Java that allows you to traverse through any type of collection, suc
5 min read
TreeSet iterator() Method in Java
The Java.util.TreeSet.iterator() method is used to return an iterator of the same elements as that of the TreeSet. The elements are returned in random order from what was present in the Tree set. Syntax: Iterator iterate_value = Tree_Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method iterates over the ele
1 min read
LinkedBlockingDeque iterator() method in Java
The iterator() method of LinkedBlockingDeque 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: Th
2 min read
Java AbstractSequentialList | iterator() method
AbstractSequentialList iterator() method in Java is used to get an Iterator or List Iterator over a list. It returns an iterator over the elements in this list (in proper sequence). This method overrides iterator() in class AbstractList<E> Syntax: public Iterator<E> iterator() Return Value: This method returns an iterator over the eleme
2 min read
HashSet iterator() Method in Java
The Java.util.HashSet.iterator() method is used to return an iterator of the same elements as the hash set. The elements are returned in random order from what present in the hash set. Syntax: Iterator iterate_value = Hash_Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method iterates over the elements of th
1 min read
PriorityQueue iterator() Method in Java
The Java.util.PriorityQueue.iterator() method is used to return an iterator of the same elements as the Priority Queue. The elements are returned in random order from what present in the Queue. Syntax: Iterator iterate_value = Priority_Queue.iterator(); Parameters: The function does not take any parameter. Return Value: The method iterates over the
2 min read
LinkedBlockingQueue iterator() method in Java
The iterator() method of LinkedBlockingQueue returns an iterator of the same elements, as this LinkedBlockingQueue, in a proper sequence. The elements returned from this method contains all the elements in order from first(head) to last(tail) of LinkedBlockingQueue. The returned iterator is weakly consistent.Syntax: public Iterator<E> iterato
3 min read
ArrayDeque iterator() Method in Java
The Java.util.ArrayDeque.iterator() method is used to return an iterator of the elements of the ArrayDeque. Syntax: Iterator iterate_value = Array_Deque.iterator(); Parameters: The method does not take any parameter. Return Value: The method iterates over the elements of the deque and returns the values(iterator). Below programs illustrate the Java
2 min read
ArrayBlockingQueue iterator() Method in Java
The iterator() method of ArrayBlockingQueue class is used to returns an iterator of the same elements as this queue in a proper sequence. The elements returned from this method contains elements in order from first(head) to last(tail). The returned iterator is weakly consistent. Syntax: public Iterator iterator() Return Value: The method returns th
2 min read
PriorityBlockingQueue iterator() method in Java
The iterator() method of PriorityBlockingQueue class Returns an iterator over the elements in this queue. The elements returned from this method do not follow any order. The returned iterator is weakly consistent. Syntax: public Iterator iterator() Parameter: This method does not take any parameter. Returns: This method returns an iterator having s
2 min read
LinkedTransferQueue iterator() method in Java
The iterator() method of java.util.concurrent.LinkedTransferQueue is an in-built function in Java which is used to return an iterator over the elements in this queue in proper sequence. Syntax: LinkedTransferQueue.iterator() Return Value: The function returns an iterator over the elements in this queue in proper sequence. Below programs illustrate
2 min read
ConcurrentSkipListSet iterator() method in Java
The iterator() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which is used to return an iterator over the elements in this set in ascending order. Syntax: ConcurrentSkipListSet.iterator() Return Value: The function returns an iterator over the elements in this set in ascending order. Below programs illustrate
2 min read
Deque iterator() method in Java
The iterator() method of Deque Interface 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: Iterator iterator() Parameters: This method does not accepts any parameter. Returns: This method
3 min read
ConcurrentLinkedQueue iterator() method in Java
The iterator() method of ConcurrentLinkedQueue is used to returns an iterator of the same elements as this ConcurrentLinkedQueue in a proper sequence. The elements returned from this method contains elements in order from first(head) to last(tail). The returned iterator is weakly consistent. Syntax: public Iterator iterator() Returns: This method r
2 min read
NavigableSet iterator() method in Java
The iterator() method of NavigableSet interface in Java is used to return an iterator over the elements in this set, in ascending order. This iterator can be then used to iterate over the elements of the set. Syntax: Iterator<E> iterator() Where, E is the type of elements maintained by this Set container. Parameters: This function does not ac
2 min read
CopyOnWriteArrayList iterator() method in Java
The iterator() method of CopyOnWriteArrayList returns an iterator over the elements in this list in proper sequence. The iterator does NOT support the remove method. Syntax: public Iterator iterator() Parameters: The function does not accept any parameters. Return Value: The function returns an iterator over the elements in the list. Below programs
2 min read
CopyOnWriteArraySet iterator() method in Java
The iterator() method of CopyOnWriteArraySet returns an iterator over the elements in this Set in proper sequence. The iterator does NOT support the remove method. Syntax: public Iterator iterator() Return Value: The function returns an iterator over the elements in the Set. Below programs illustrate the above function: Program 1: // Java Program t
2 min read
Stack iterator() method in Java with Example
The Java.util.Stack.iterator() method is used to return an iterator of the same elements as that of the Stack. The elements are returned in random order from what was present in the stack. Syntax: Iterator iterate_value = Stack.iterator(); Parameters: The function does not take any parameter. Return Value: The method iterates over the elements of t
2 min read
ConcurrentLinkedDeque iterator() method in Java with Example
The Java.util.concurrent.ConcurrentLinkedDeque.iterator() method is used to return an iterator of the same elements as that of the ConcurrentLinkedDeque. The elements are returned in random order from what was present in the deque. Syntax: Iterator iterate_value = ConcurrentLinkedDeque.iterator(); Parameters: The function does not take any paramete
2 min read
IntStream iterator() in Java
IntStream iterator() returns an iterator for the elements of this stream. It is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : PrimitiveIterator.OfInt iterator() Where, PrimitiveIterator.OfInt is an Iterator specialized for int values. Return Value : IntStream iterator() returns the element itera
2 min read
Java | Implementing Iterator and Iterable Interface
Iterators are used in Collection framework in Java to retrieve elements one by one. For more details and introduction related to this, see this link. Why it is needed to implement Iterable interface? Every class that implements Iterable interface appropriately, can be used in the enhanced For loop (for-each loop). The need to implement the Iterator
4 min read
Iterator vs Collection in Java
Iterator and Collection, both has helped and comforted the programmers at many a times. But their usage and application has a very wide difference. 1. Iterator Declaration public interface Iterator Type Parameters: E - the type of elements returned by this iteratorIterators are used in Collection framework in Java to retrieve elements one by one. M
3 min read
three90RightbarBannerImg