📌  相关文章
📜  Java中的 ConcurrentSkipListSet iterator() 方法

📅  最后修改于: 2022-05-13 01:55:33.078000             🧑  作者: Mango

Java中的 ConcurrentSkipListSet iterator() 方法

Java .util.concurrent.ConcurrentSkipListSetiterator()方法是Java中的一个内置函数,用于按升序返回此集合中元素的迭代器。
句法:

ConcurrentSkipListSet.iterator()  

返回值:该函数以升序返回此集合中元素的迭代器。

下面的程序说明了Java.util.concurrent.ConcurrentSkipListSet.iterator() 的使用:
方案一:

// 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
            set = new ConcurrentSkipListSet();
  
        // Adding elements to this set
        set.add("Gfg");
        set.add("is");
        set.add("fun!!");
  
        // Returns an iterator over the elements
        Iterator iterator = set.iterator();
  
        // Printing the elements of the set
        while (iterator.hasNext())
            System.out.print(iterator.next() + " ");
    }
}
输出:
Gfg fun!! is

方案二:

// 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
            set = new ConcurrentSkipListSet();
  
        // Adding elements to this set
        set.add(10);
        set.add(15);
        set.add(20);
        set.add(25);
  
        // Returns an iterator over the elements
        Iterator iterator = set.iterator();
  
        // Printing the elements of the set
        while (iterator.hasNext())
            System.out.print(iterator.next() + " ");
    }
}
输出:
10 15 20 25

参考: https: Java/util/concurrent/ConcurrentSkipListSet.html#iterator–