TreeMap Map.Entry pollFirstEntry() Method Example Program


Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.

Program

package com.candidjava;

import java.util.TreeMap;

/**
 * @author: Hari Haran
 * @discription: The pollFirstEntry() method is used to remove and returns a
 *               key-value mapping associated with the least key in this map, or
 *               null if the map is empty.
 */

public class TreeMapPollFirstEntry {
	public static void main(String[] args) {
		TreeMap tm = new TreeMap();

		tm.put(1, "Anand");
		tm.put(2, "Hari Haran");
		tm.put(8, "Kamal");
		tm.put(4, "Karthikeyan");
		tm.put(5, "Mohan");
		tm.put(6, "Vinoth");
		tm.put(7, "Hari Haran");

		System.out.println("Value before poll: " + tm);
		System.out.println("Value returned: " + tm.pollFirstEntry());
		System.out.println("Value after poll: " + tm);
	}
}

Output

Value before poll: {1=Anand, 2=Hari Haran, 4=Karthikeyan, 5=Mohan, 6=Vinoth, 7=Hari Haran, 8=Kamal}
Value returned: 1=Anand
Value after poll: {2=Hari Haran, 4=Karthikeyan, 5=Mohan, 6=Vinoth, 7=Hari Haran, 8=Kamal}

Explanation

public Map.Entry pollFirstEntry()
Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.
Specified by:
pollFirstEntry in interface NavigableMap
Returns:
the removed first entry of this map, or null if this map is empty


Related Post

Comments


©candidjava.com