firstKey() method

firstKey(): It returns the key from TreeMap which have the least value(Key having lowest value) . It returns null if TreeMap is empty.

import java.util.TreeMap;
class ExampleOfTreeMap
	public static void main(String arg[])
		TreeMap<Integer, String> listOfNames = new TreeMap<Integer, String>();
		listOfNames.put(3, "C");
		listOfNames.put(1, "A");
		listOfNames.put(2, "D");
		listOfNames.put(6, "B");
		listOfNames.put(8, "B");
		// It return the Key having least Key of TreeMap
		System.out.println("Least Key of TreeMap :"+listOfNames.firstKey());

Output: Least Key of TreeMap :1

Leave a Comment

Follow us on Instagram & watch the latest videos on YouTube. Click below social icons to visit our Instagram & YouTube profiles.