TreeMap Map.Entry pollLastEntry() Method Example Program


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

Program

package com.candidjava;

import java.util.TreeMap;

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

public class TreeMapPollLastEntry {
	public static void main(String[] args) {
		TreeMap<Integer, String> tm = new TreeMap<Integer, String>();

		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.pollLastEntry());
		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: 8=Kamal
Value after poll: {1=Anand, 2=Hari Haran, 4=Karthikeyan, 5=Mohan, 6=Vinoth, 7=Hari Haran}

Explanation

public Map.Entry<K,V> pollLastEntry()
Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
Specified by:
pollLastEntry in interface NavigableMap<K,V>
Returns:
the removed last entry of this map, or null if this map is empty


Related Post

Comments


©candidjava.com