Map


 

                         Java Map is part of collections framework. Java Map object is used to store key-value mappings. Java Map can’t contain duplicate keys however duplicate values are allowed.

1. Hash Map:

  • Java HashMap allows single null key and null values. 
  • HashMap is not an ordered collection. You can iterate over HashMap entries through keys set but they are not guaranteed to be in the order of their addition to the HashMap. 
  • HashMap is almost similar to Hashtable except that it’s unsynchronized and allows null key and values. 
  • HashMap uses it’s inner class Node for storing map entries. 
  • HashMap stores entries into multiple singly linked lists, called buckets or bins. Default number of bins is 16 and it’s always power of 2.

                  HashMap uses hashCode() and equals() methods on keys for get and put operations. So HashMap key object should provide good implementation of these methods. This is the reason immutable classes are better suitable for keys, for example String and Integer.

                    package com.csi.collectionconcept; 

                               import java.util.HashMap; 

                               import java.util.Map; 

                               public class HashMapConcept {

                                              public static void main(String[] args) { 

                                                           HashMap hm = new HashMap<>(); 

                                                           hm.put("ID", "121"); 

                                                           hm.put("NAME", "JERRY"); 

                                                           hm.put("SALARY", "96000.99"); 

                                                           hm.put("ADDRESS", "PUNE"); 

                                                           for (Map.Entry m : hm.entrySet()) { 

                                                                             System.out.println(m.getKey() + ": " + m.getValue()); 

                                                             } 

                                             } 

                           }

 

                          Output: 

                                               SALARY: 96000.99 

                                                 ADDRESS: PUNE 

                                                 ID: 121 

                                                 NAME: JERRY

 

2. Tree Map:

  • Java TreeMap contains only unique elements. 
  • Java TreeMap cannot have a null key but can have multiple null values. 
  • Java TreeMap is non-synchronized. 
  • Java TreeMap maintains ascending order.

                  package com.csi.collectionconcept; 

                           import java.util.Map; 

                           import java.util.TreeMap; 

                           public class TreeMapConcept { 

                                            public static void main(String[] args) { 

                                                            TreeMap tm = new TreeMap<>(); 

                                                            tm.put("ID", "121"); 

                                                            tm.put("NAME", "JERRY"); 

                                                            tm.put("SALARY", "96000.99");

                                                            tm.put("ADDRESS", "PUNE"); 

                                                            for (Map.Entry m : tm.entrySet()) { 

                                                                         System.out.println(m.getKey() + ": " + m.getValue()); 

                                                             } 

                                              } 

                              }

 

Output: 

                                          ADDRESS: PUNE 

                                          ID: 121 

                                          NAME: JERRY 

                                          SALARY: 96000.99