treemap get value java code example

Example 1: TreeMap get() method in java

map Integer values to String Keys
import java.util.TreeMap;
public class TreeMapGetMethodExample
{
   public static void main(String[] args)
   {
      TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
      tm.put("violet", 69);
      tm.put("indigo", 65);
      tm.put("green", 40);
      tm.put("blue", 95);
      tm.put("orange", 28);
      System.out.println("TreeMap Mappings: " + tm);
      // get the value of "violet"
      System.out.println("Value is: " + tm.get("violet"));
      // get the value of "green"
      System.out.println("Value is: " + tm.get("green"));
   }
}

Example 2: TreeMap get() method in java

import java.util.TreeMap;
public class TreeMapGetMethodExample
{
   public static void main(String[] args)
   {
      TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
      tm.put(69, "violet");
      tm.put(65, "indigo");
      tm.put(40, "green");
      tm.put(95, "blue");
      tm.put(28, "orange");
      System.out.println("TreeMap Mappings: " + tm);
      // get the value of 69
      System.out.println("Value is: " + tm.get(69));
      // get the value of 95
      System.out.println("Value is: " + tm.get(95));
   }
}

Tags:

Java Example