TreeMap Map.Entry lastEntry() Method Example Program


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: ThelastEntry() method is used to return a key-value mapping
 *               associated with the greatest key in this map, or null if the
 *               map is empty.
 */

public class TreeMapLastEntry {
	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("Checking last entry");
		System.out.println("Value is: " + tm.lastEntry());
	}
}

Output

Checking last entry
Value is: 8=Kamal

Explanation

public Map.Entry<K,V> lastEntry()
Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
Specified by:
lastEntry in interface NavigableMap<K,V>
Returns:
an entry with the greatest key, or null if this map is empty


Related Post

Comments


©candidjava.com