当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java TreeSet pollFirst()用法及代码示例


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: []


相关用法


注:本文由纯净天空筛选整理自ankit15697大神的英文原创作品 TreeSet pollFirst() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。