share

Friday, 19 June 2015

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





No comments:

Post a Comment