ConcurrentSkipListSet的lower()方法用于返回此集合中存在的最大元素,该元素严格小于指定元素。如果集合中不存在此类元素,则此函数将返回null。
用法:
public E lower (E e)
参数:此方法仅采用一个参数,e是要匹配的指定值。
返回值:此方法返回的值是集合中存在的最大元素,该值严格小于指定元素。如果不存在此类元素,则它将返回Null。
异常:此方法引发以下异常:
- ClassCastException:如果此集合的元素的类与指定的集合不兼容,则抛出此错误。
- NullPointerException :如果指定的集合为null,则抛出此错误。
以下示例程序旨在说明ConcurrentSkipListSet类的lower()函数:
示例1:在下面的程序中,指定的元素为67,我们的集合也包含元素67,但是由于lower()方法返回的值严格较少,因此未返回该元素。
// Java program to demonstrate
// ConcurrentSkipListSet lower() method.
import java.util.concurrent.ConcurrentSkipListSet;
class GfgCSLS {
public static void main(String[] args)
{
// Initializing the set
// using ConcurrentSkipListSet()
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
// Adding elements to this set
// using add() method
set.add(17);
set.add(24);
set.add(35);
set.add(67);
set.add(98);
// Printing the ConcurrentSkipListSet
System.out.println("ConcurrentSkipListSet: "
+ set);
// Invoking lower() method
// to find the largest element
// less than the specified element
System.out.println("Largest element: "
+ set.lower(67));
}
}
输出:
ConcurrentSkipListSet: [17, 24, 35, 67, 98] Largest element: 35
示例2:显示NullpointerException
// Java program to demonstrate
// ConcurrentSkipListSet lower() method.
import java.util.concurrent.ConcurrentSkipListSet;
class GfgCSLS {
public static void main(String[] args)
{
// Initializing the set
// using ConcurrentSkipListSet()
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
// Adding elements to this set
// using add() method
set.add(17);
set.add(24);
set.add(35);
set.add(67);
set.add(98);
// Printing the ConcurrentSkipListSet
System.out.println("ConcurrentSkipListSet: "
+ set);
try {
// Invoking lower() method
// to find the largest element
// less than the specified element
System.out.println("Largest element: "
+ set.lower(null));
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
ConcurrentSkipListSet: [17, 24, 35, 67, 98] java.lang.NullPointerException
相关用法
- Java ConcurrentSkipListSet subSet()用法及代码示例
- Java ConcurrentSkipListSet tailSet()用法及代码示例
- Java ConcurrentSkipListSet comparator()用法及代码示例
- Java ConcurrentSkipListSet add()用法及代码示例
- Java ConcurrentSkipListSet contains()用法及代码示例
- Java ConcurrentSkipListSet last()用法及代码示例
- Java ConcurrentSkipListSet first()用法及代码示例
- Java ConcurrentSkipListSet pollFirst()用法及代码示例
- Java ConcurrentSkipListSet descendingIterator()用法及代码示例
- Java ConcurrentSkipListSet subSet()用法及代码示例
- Java ConcurrentSkipListSet removeAll()用法及代码示例
- Java ConcurrentSkipListSet iterator()用法及代码示例
- Java ConcurrentSkipListSet floor()用法及代码示例
- Java ConcurrentSkipListSet pollLast()用法及代码示例
注:本文由纯净天空筛选整理自ProgrammerAnvesh大神的英文原创作品 ConcurrentSkipListSet lower() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。