Sometimes in java while we do coding and use HashMap we may need to check or see the values reside in Key and their corresponding Data or Values at the same time. Here I am sharing you another simple but useful technique to see the key value pair data of HashMap at a time. Just copy the code below and run it on your computer.
Here is the code:
import java.util.HashMap;
public class HashMap1
{
public static void main( String[] args )
{
HashMap map = new HashMap();
map.put(new Integer(10), “Jhony” );
map.put(new Integer(11), “Tom” );
map.put(new Integer(12), “Tom” );
System.out.println(“\n****************************************”);
System.out.println(“\nKey Value Pair of HashMap 1st Way :”+map);
System.out.println(“\nKey Used in HashMap :”+map.keySet());
System.out.println(“\nKey Value Pair of HasMap in 2nd Way :”+map.entrySet());
System.out.println(“\nValue Used in HashMap :”+map.values());
System.out.println(“\n****************************************”);
}
}
Hi,
Thanks for htis Nice artilce just to add while discussing about HashMap its worth mentioning following questions which frequently asked in Java interviews now days like How HashMap works in Java or How get() method of HashMap works in JAVA very often. on concept point of view these questions are great and expose the candidate if doesn’t know deep details.
Javin
LikeLike