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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。