How to get value of key in TreeMap

import java.util.Map;
import java.util.TreeMap;

public class TreeMapGetValueOfkey {

public static void main(String[] args) {

Map<Integer, String> tm = new TreeMap<Integer,String>();

// add key and values
tm.put(1,”java”);
tm.put(2,”cpp”);
tm.put(3,”php”);
tm.put(4,”c”);
tm.put(5, “mysql”);

/*
* Returns the value to which the specified key is
mapped, or null if this map contains no mapping for
the key.
*/
System.out.println(“key =1  and value =”+tm.get(1));
System.out.println(“key =1 and value =”+tm.get(5));
System.out.println(“key =1 and value =”+tm.get(10));

}

}

Output:

key =1  and value =java

key =1 and value =mysql

key =1 and value =null

Leave a Reply

Your email address will not be published. Required fields are marked *

74 − 66 =