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


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


Java.util.concurrent.TreeSet的pollLast()方法是Java中的内置函数,该函数返回检索并删除最后一个(最高)元素,如果此集为空,则返回null。

用法:

public E pollLast()

返回值:该函数返回以检索并删除最后一个(最高)元素,如果此集合为空,则返回null。


以下示例程序旨在说明TreeSet.pollLast()方法:

示例1:

// Java program to demonstrate pollLast() 
// method of TreeSet 
  
import java.util.*; 
  
class TreeSetpollLastExample1 { 
    public static void main(String[] args) 
    { 
        // Creating a set object 
        TreeSet<Integer> set 
            = new TreeSet<Integer>(); 
  
        // Adding elements to this set 
        for (int i = 10; i <= 50; i += 10) 
            set.add(i); 
  
        // Printing the content of the set 
        System.out.println("Contents of the set: " + set); 
  
        // Retrieving and removing Last element of the set 
        System.out.println("The Last element of the set: "
                           + set.pollLast()); 
  
        // Printing the content of the set after pollLast() 
        System.out.println("Contents of the set after pollLast: "
                           + set); 
    } 
}
输出:
Contents of the set: [10, 20, 30, 40, 50]
The Last element of the set: 50
Contents of the set after pollLast: [10, 20, 30, 40]

示例2:

// Java program to demonstrate pollLast() 
// method of TreeSet 
  
import java.util.*; 
  
class TreeSetpollLastExample2 { 
    public static void main(String[] args) 
    { 
        // Creating a set object 
        TreeSet<Integer> set 
            = new TreeSet<Integer>(); 
  
        // Printing the content of the set 
        System.out.println("Contents of the set: "
                           + set); 
  
        // Retrieving and removing Last element of the set 
        System.out.println("The Last element of the set: "
                           + set.pollLast()); 
    } 
}
输出:
Contents of the set: []
The Last element of the set: null


相关用法


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