Java TreeMap pollFirstEntry()方法

java.util.TreeMap.pollFirstEntry() 删除并返回与Map中最小键关联的key-value映射;如果映射为空,则返回null。

1 语法

public Map.Entry<K,V> pollFirstEntry()

2 参数

3 返回值

返回此映射的已删除第一项;如果此映射为空,则返回null。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.TreeMap.pollFirstEntry()方法的例子
 */
import java.util.*;

public class Demo {
    public static void main(String[] args) {

        // creating tree map
        TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();

        // populating tree map
        treemap.put(2, "two");
        treemap.put(1, "one");
        treemap.put(3, "three");
        treemap.put(6, "six");
        treemap.put(5, "five");

        // polling first entry
        System.out.println("Value before poll: "+ treemap);
        System.out.println("Value returned: "+ treemap.pollFirstEntry());
        System.out.println("Value after poll: "+ treemap);
    }
}

输出结果为:

Value before poll: {1=one, 2=two, 3=three, 5=five, 6=six}
Value returned: 1=one
Value after poll: {2=two, 3=three, 5=five, 6=six}

热门文章

优秀文章