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

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

Java中的 ConcurrentSkipListSet headSet() 方法

headSet(E toElement)

Java .util.concurrent.ConcurrentSkipListSet.headSet()方法是Java中的一个内置函数,它返回该集合中元素严格小于 toElement 的部分的视图。返回的集合由该集合支持,因此返回集合中的更改会反映在该集合中,反之亦然。返回的集合支持该集合支持的所有可选集合操作。

句法:

public NavigableSet headSet(E toElement)

参数:该函数接受单个参数toElement ,即返回集的高端点。

返回值:该函数返回一个NavigableSet ,它是该集合中元素严格小于 toElement 的部分的视图。

下面的程序说明了 ConcurrentSkipListSet.headSet(E toElement) 方法:
方案一:

// Java Program Demonstrate headSet()
// method of ConcurrentSkipListSet */
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetHeadSetExample1 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet
            set = new ConcurrentSkipListSet();
  
        // Adding elements to this set
        for (int i = 10; i <= 50; i += 10)
            set.add(i);
  
        // Creating a headSet object with upper limit 30
        NavigableSet hd_set = set.headSet(30);
  
        // Printing the elements of the set
        System.out.println("Contents of the set: " + set);
  
        // Printing the elements of the headSet set
        System.out.println("Contents of the headset"
                           + " with upper limit 30: "
                           + hd_set);
    }
}
输出:
Contents of the set: [10, 20, 30, 40, 50]
Contents of the headset with upper limit 30: [10, 20]

程序 2:显示 NullPointerException 的程序。

// Java Program Demonstrate headSet()
// method of ConcurrentSkipListSet */
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetHeadSetExample2 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet
            set = new ConcurrentSkipListSet();
  
        // Adding elements to this set
        for (int i = 10; i <= 50; i += 10)
            set.add(i);
  
        // Printing the elements of the set
        System.out.println("Contents of the set: " + set);
  
        try {
            // Trying to creating a headSet object with upper limit null
            NavigableSet hd_set = set.headSet(null);
        }
  
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
        // Printing the elements of the headSet set
        // System.out.println("Contents of the headset"
+" with upper limit 30: "+ hd_set);
    }
}
输出:
Contents of the set: [10, 20, 30, 40, 50]
Exception: java.lang.NullPointerException

headSet(E toElement, boolean inclusive)

Java .util.concurrent.ConcurrentSkipListSet.headSet()方法是Java中的一个内置函数,它返回该集合中元素小于(或等于,如果 inclusive 为真)toElement 的部分的视图。返回的集合由该集合支持,因此返回集合中的更改会反映在该集合中,反之亦然。返回的集合支持该集合支持的所有可选集合操作。

句法:

public NavigableSet headSet(E toElement,
                      boolean inclusive)

参数:该函数接受以下参数

  • toElement即返回集合的高端点。
  • inclusive – 如果要在返回的视图中包含高端端点,则为 true。

    返回值:该函数返回一个 NavigableSet,它是该集合中元素小于(或等于,如果 inclusive 为真)toElement 的部分的视图。

    下面的程序说明了 ConcurrentSkipListSet.headSet(E toElement, boolean inclusive) 方法:

    方案 3:

    // Java Program Demonstrate headSet()
    // method of ConcurrentSkipListSet */
      
    import java.util.NavigableSet;
    import java.util.concurrent.ConcurrentSkipListSet;
      
    class ConcurrentSkipListSetHeadSetExample3 {
        public static void main(String[] args)
        {
      
            // Initializing the set
            ConcurrentSkipListSet
                set = new ConcurrentSkipListSet();
      
            // Adding elements to this set
            for (int i = 10; i <= 50; i += 10)
                set.add(i);
      
            // Creating a headSet object with upper limit 30 inclusive
            NavigableSet hd_set = set.headSet(30, true);
      
            // Printing the elements of the set
            System.out.println("Contents of the set: " + set);
      
            // Printing the elements of the headSet set
            System.out.println("Contents of the headset"
                               + " with upper limit 30 inclusive: "
                               + hd_set);
        }
    }
    
    输出:
    Contents of the set: [10, 20, 30, 40, 50]
    Contents of the headset with upper limit 30 inclusive: [10, 20, 30]
    

    参考: https: Java/util/concurrent/ConcurrentSkipListSet.html#headSet-E-