TreeMap K firstKey() Method Example Program


Returns the first (lowest) key currently in this map.

Program

package com.candidjava;

import java.util.TreeMap;

/**
 * @author: Hari Haran
 * @discription:The firstKey() method is used to return the first (lowest) key
 *                  currently in this map.
 */

public class TreeMapFirstKey {
	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 first key");
		System.out.println("First key is: " + tm.firstKey());
	}
}

Output

Checking first key
First key is: 1

Explanation

public K firstKey()
Returns the first (lowest) key currently in this map.
Specified by:
firstKey in interface SortedMap<K,V>
Returns:
the first (lowest) key currently in this map
Throws:
NoSuchElementException - if this map is empty


Related Post

Comments


©candidjava.com