TreeMap entrySet() method in java code example

Example 1: TreeMap entrySet() method in java

example to map integer values to String keys
import java.util.TreeMap;
public class TreeMapEntrySetMethodExample
{
   public static void main(String[] args)
   {
      TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
      // map Integer values to String keys
      tm.put("mango", 56);
      tm.put("apple", 28);
      tm.put("banana", 35);
      tm.put("pineapple", 73);
      tm.put("orange", 61);
      // printing the TreeMap
      System.out.println("Given TreeMap is: " + tm);
      // use entrySet() to get set view
      System.out.println("set view is: " + tm.entrySet());
   }
}

Example 2: TreeMap entrySet() method in java

import java.util.TreeMap;
public class TreeMapEntrySetMethodExample
{
   public static void main(String[] args)
   {
      TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
      // map String values to Integer keys
      tm.put(56, "mango");
      tm.put(28, "apple");
      tm.put(35, "banana");
      tm.put(73, "pineapple");
      tm.put(61, "orange");
      // printing the TreeMap
      System.out.println("Given TreeMap is: " + tm);
      // use entrySet() to get set view
      System.out.println("set view is: " + tm.entrySet());
   }
}

Tags:

Java Example