Saturday, October 25, 2025
HomeLanguagesJavaConcurrentSkipListSet iterator() method in Java

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 the use of java.util.concurrent.ConcurrentSkipListSet.iterator() :
Program 1:




// Java Program Demonstrate iterator()
// method of ConcurrentSkipListSet 
  
import java.util.Iterator;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetIteratorExample1 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet<String>
            set = new ConcurrentSkipListSet<String>();
  
        // Adding elements to this set
        set.add("Gfg");
        set.add("is");
        set.add("fun!!");
  
        // Returns an iterator over the elements
        Iterator<String> iterator = set.iterator();
  
        // Printing the elements of the set
        while (iterator.hasNext())
            System.out.print(iterator.next() + " ");
    }
}


Output:

Gfg fun!! is

Program 2:




// Java Program Demonstrate iterator()
// method of ConcurrentSkipListSet */
  
import java.util.Iterator;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetIteratorExample2 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet<Integer>
            set = new ConcurrentSkipListSet<Integer>();
  
        // Adding elements to this set
        set.add(10);
        set.add(15);
        set.add(20);
        set.add(25);
  
        // Returns an iterator over the elements
        Iterator<Integer> iterator = set.iterator();
  
        // Printing the elements of the set
        while (iterator.hasNext())
            System.out.print(iterator.next() + " ");
    }
}


Output:

10 15 20 25

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#iterator–

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS