Java中TreeSet的pollFirst()方法用于检索和删除第一个(最低)元素,如果TreeSet为空,则返回null。
用法:
E pollFirst()
其中,E是此TreeSet容器维护的元素的类型。
参数:该函数不接受任何参数。
返回类型:如果TreeSet不为空,则返回Treeset的第一个元素,否则返回null。
以下程序说明了Java中的pollFirst()方法:
例子1
// A Java program to demonstrate
// pollFirst() method of TreeSet
import java.util.TreeSet;
public class GFG {
public static void main(String[] args)
{
// creating TreeSet
TreeSet<Integer> ts = new TreeSet<Integer>();
// adding element to the TreeSet
ts.add(0);
ts.add(1);
ts.add(2);
ts.add(3);
ts.add(4);
ts.add(5);
ts.add(6);
// before removing element
System.out.println("Before removing " +
"element from TreeSet: " + ts);
// first element is removed
System.out.println("First lowest element " +
"removed is : " + ts.pollFirst());
System.out.println("After removing element" +
" from TreeSet: " + ts);
}
}
输出:
Before removing element from TreeSet: [0, 1, 2, 3, 4, 5, 6] First lowest element removed is : 0 After removing element from TreeSet: [1, 2, 3, 4, 5, 6]
例子2
// A Java program to demonstrate
// pollFirst() method of TreeSet
import java.util.TreeSet;
public class GFG {
public static void main(String[] args)
{
// creating TreeSet
TreeSet<String> ts = new TreeSet<String>();
// removing element when TreeSet
// is empty
System.out.println("pollFirst() method when " +
"TreeSet is empty : " + ts.pollFirst());
// adding element to the TreeSet
ts.add("Geeks");
ts.add("for");
ts.add("Geek");
// before removing element
System.out.println("Before removing element from" +
" TreeSet: " + ts);
// first element is removed
System.out.println("First lowest element removed is : "
+ ts.pollFirst());
System.out.println("After removing element " +
"from TreeSet: " + ts);
}
}
输出:
pollFirst() method when TreeSet is empty : null Before removing element from TreeSet: [Geek, Geeks, for] First lowest element removed is : Geek After removing element from TreeSet: [Geeks, for]
如果TreeSet为空,则此方法返回null。
例子3
// A Java program to demonstrate
// pollFirst() method of TreeSet
import java.util.TreeSet;
public class GFG {
public static void main(String[] args)
{
// creating TreeSet
TreeSet<Integer> ts = new TreeSet<Integer>();
// before removing element
System.out.println("Before removing " +
"element from TreeSet: " + ts);
// first element is removed
System.out.println("First lowest element " +
"removed is : " + ts.pollFirst());
System.out.println("After removing element" +
" from TreeSet: " + ts);
}
}
输出:
Before removing element from TreeSet: [] First lowest element removed is : null After removing element from TreeSet: []
相关用法
- Java ArrayDeque pollFirst()用法及代码示例
- Java LinkedBlockingDeque pollFirst()用法及代码示例
- Java NavigableSet pollFirst()用法及代码示例
- Java ConcurrentLinkedDeque pollFirst()用法及代码示例
- Java ConcurrentSkipListSet pollFirst()用法及代码示例
- Java BlockingDeque pollFirst()用法及代码示例
- Java TreeSet add()用法及代码示例
- Java TreeSet contains()用法及代码示例
- Java TreeSet first()用法及代码示例
- Java TreeSet last()用法及代码示例
- Java TreeSet lower()用法及代码示例
- Java TreeSet subSet()用法及代码示例
- Java TreeSet clone()用法及代码示例
- Java TreeSet clear()用法及代码示例
注:本文由纯净天空筛选整理自ankit15697大神的英文原创作品 TreeSet pollFirst() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。