One of the most important Java topics is Generics/Collections.Collections are nothing but data structures just in C++ and C . Collections class has several methods that allows you to return/operate on collections like List,Set,Map,SortedMap,Enumeration,ListOperator,…etc. Today we’ll have a look at one of the most discussed topics in Collections Framework - The HashMap.
What is HashMap in Java?
HashMap in Java is the collection framework class that extends the AbstractMap class and implements the Serializable,Cloneable and Map interfaces. HashMap in Java are quite similar to the HashTable (as it store elements in the key , value format just as the HashTable) , the key point of differences between HashMap and HashTable (HashMap vs HashTable) is that order of elements in HashMap’s are not synchronized (which makes it susceptible to deadlocks ),its elements order are not preserved and it can store Null values too. Make sure that you remember these points on Hashmap in Java as it’s one of the most favorite topics of interviewers.
Must Read :- FlowLayout Java Swing Tutorial
Hashmap in Java Example to Sort Elements by Key
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.io.*;
class hshImplement {
String ss=null;
int in=0,i=0,xx=0;
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
Map mp=new HashMap<Integer,String>();
Iterator rit;
protected void readElements()
{
try{
for(int i=0;i<3;i++)
{
System.out.println("Enter the Integer Key for the "+i+1+" element");
in=Integer.parseInt(bf.readLine());
System.out.println("Enter the String value for the "+i+1+" element");
ss=bf.readLine();
mp.put(in,ss);
}
}catch(Exception x)
{
System.out.println(x.getMessage());
}
}
protected void displayElements(String sort)
{
switch(sort)
{
case "Yes" :
System.out.println("Entered");
Map trmp=new TreeMap<Integer,String>(mp);
rit=trmp.entrySet().iterator();
xx=0;
while(rit.hasNext())
{
Entry mpntry=(Entry)rit.next();
System.out.println("Element "+xx+" Key="+mpntry.getKey()+" value="+mpntry.getValue());
}
break;
}
}
}
public class hshExample {
public static void main(String args[])
{
hshImplement hi=new hshImplement();
hi.readElements();
System.out.println("After sorting");
hi.displayElements("Yes");
}
}
Must Read :- GridLayout Java Swing Tutorial
Output :
Enter the Integer Key for the 0 element
2
Enter the String value for the 0 element
asd
Enter the Integer Key for the 1 element
1
Enter the String value for the 1 element
aasdadsad
Enter the Integer Key for the 2 element
4
Enter the String value for the 2 element
asdad
After sorting
Entered
Element 0 Key=1 value=aasdadsad
Element 0 Key=2 value=asd
Element 0 Key=4 value=asdad
Explanation of the above code :-
hshExample is our main class that instantiates the hshImplement class . The hshImplement class has the method readElements() which will read user input and store it in our HashMap mp and the displayElements(String sort) method has a switch case statement which switch’s to the “Yes” case where the Elements get sorted and displayed (as shown in the output) .
