HashMap Sort Get Java


Three programs are given on HashMap with different functionalities used very often in coding.

  1. HashMap General: In this program, API and the general operations like adding, retrieving and deleting elements, size and checking the existence of elements are done.
  2. HashMap Iteration: The iteration styles of elements, adding elements of one HashMap to another and cloning are shown.
  3. HashMap Sort: Sorting operations on keys and values are done. Special attention is paid to retrieve the key when a value is given.

Note: It is advised to read the tutorial on HashMap before attempting the above programs as it gives the knowledge on features, methods and super classes of HashMap etc.

It is the third program of the above three.

Example on HashMap Sort
import java.util.*;
public class HashMapSort
{
  public static void main(String args[])
  {
    HashMap hm1 = new HashMap();
    hm1.put("one", 1);
    hm1.put("thousand", 1000);
    hm1.put("ten", 10);
    hm1.put("hundred", 100);
		                                       // HASHMAP SORT KEYS
    Set mySet = hm1.keySet();
    System.out.println("\nhm1 keys: " + mySet);
    TreeSet ts1 = new TreeSet(mySet);
    System.out.println("hm1 sorted keys: " + ts1);

		              // SORTING VALUES
    Collection myCol = hm1.values();
    System.out.println("\nhm1 values: " + myCol);  	
    TreeSet ts2 = new TreeSet(myCol);
    System.out.println("hm1 sorted values: " + ts2);
		                                       // GET KEY FROM VALUE
    for(Object obj1 : mySet)
    {
      if(hm1.get(obj1).equals(10))
      {
        System.out.println("10 value key is " + obj1);   
      }
    }
  }
}    


HashMap Sort
Output Screen of HashMap Sort Get Java

     HashMap hm1 = new HashMap();
     hm1.put("one", 1);

A non-generic HashMap object hm1 is created and added a few key/value pairs with put() method.

     Set mySet = hm1.keySet();
     TreeSet ts1 = new TreeSet(mySet);  // HashMap sort
     System.out.println("hm1 sorted keys: " + ts1);

There are two operations in HashMap, printing the keys and values in sorted ordered manner. For this keySet() and values() methods are used that return only keys and only values stored in HashMap. The keySet() returns an object of Set which does not accept duplicate elements. The Set object mySet is passed to the constructor of TreeSet and TreeSet nature is to print the elements in ascending order by default.

     Collection myCol = hm1.values();
     TreeSet ts2 = new TreeSet(myCol);
     System.out.println("hm1 sorted values: " + ts2);

The same earlier technique is used to the print the values in ascending order. The values() method returns an object of Collection myCol and is passed to TreeSet constructor ts2 and ts2 is printed. The values are printed in ascending order by default.

		                     // GET KEY FROM VALUE
     for(Object obj1 : mySet)
     {
       if(hm1.get(obj1).equals(10))
       {
         System.out.println("10 value key is " + obj1);   
       }
     }

By supplying the key to get() method, we retrieve the value associated. But the reverse is not available with Map interface; that is, when value is supplied to get the key. For this, a small code is required to write. The earlier keySet object contains all the keys of HashMap hm1. For each operation, check the value associated with a particular key and if the value matches with our required value, then return the key. The required value 10 is passed to equals() as parameter.

Leave a Comment

Your email address will not be published.