Java ConcurrentSkipListSet headSet()方法

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)方法:
程序1:

// 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<Integer>
            set = new ConcurrentSkipListSet<Integer>();
  
        // 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<Integer> 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<Integer>
            set = new ConcurrentSkipListSet<Integer>();
  
        // 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<Integer> 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中的一个内置函数,它返回这个集合中元素小于(或等于,如果包容性为真)toElement的部分的一个视图。返回的集合是由这个集合支持的,所以返回的集合的变化会反映在这个集合中,反之亦然。返回的集合支持这个集合所支持的所有可选的集合操作。

语法

public NavigableSet headSet(E toElement,
                      boolean inclusive)

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

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

返回值: 该函数返回一个NavigableSet,它是这个集合中元素小于(或等于,如果包容性为真)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<Integer>
            set = new ConcurrentSkipListSet<Integer>();
  
        // 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<Integer> 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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#headSet-E-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程