Java HashMap Class

  • Is is pronounced as HashMap because It is hash table based implementation of map interface.
  • It extends AbstractMap class and implements Map interface.
  • It allows to store null key and null values.
  • It is non synchronized implementation so if multiple threads accessing it concurrently and any one of them modifies it then it must be synchronized externally.
  • It does not guarantee of maintaining the map order.
Important methods of HashMap Class are as below.
  • void clear() : It will remove and clear all mapping entries from map.
  • Object clone() : It will provide you shallow copy of this map.
  • boolean containsKey(Object key) : It will check and return true if specified key has any mapping in this map.
  • boolean containsValue(Object value) : It will check and return true if any key has mapping with given value.
  • Set<Map.Entry<K,V>> entrySet() : It will return the set view of mapping.
  • V get(Object key) : get method is used for retrieving value of specified key from this map. If no mapping found, It will return null.
  • boolean isEmpty() : It will check and return true if map is empty.
  • Set<K> keySet() : It will return set view of the keys of this map.
  • V put(K key, V value) : It will associate specified key value mapping in this map.
  • void putAll(Map<? extends K,? extends V> m) : It will copy all key-value mapping pair of specified map to this map.
  • V remove(Object key) : It will remove mapping of specified key from this map.
  • int size() :It will return size of this map.
  • Collection<V> values() : It will return collection view of map values.

HashMapExample :
package JAVAExamples;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapExample {

 public static void main(String[] args) {
  // create Map
  Map<Integer, String> m = new HashMap<Integer, String>();
  // Add key with value in map.
  m.put(3,"Chicago");
  m.put(7,"Mumbai");
  m.put(1,"Tokyo");
  m.put(5,"Delhi");
  m.put(2,"Paris"); 
  m.put(8,"Beijing");
  m.put(11,"Berlin");

  //Print map
  System.out.println("Map : "+m);
  // Get size of map.
  System.out.println("Map size is : " + m.size()); 

  //Check if map is empty.
  System.out.println("Map is empty? : "+m.isEmpty());
  
  //Remove mapping from map.
  m.remove(3);
  System.out.println("Map value after removing mapping of key 3 : "+m);
  
  //Get value from map for specified key.
  System.out.println("Value for key 5 is : "+m.get(5));
  
  //Iterating over map.
  Iterator<Integer> keySetIterator = m.keySet().iterator();
  while(keySetIterator.hasNext()){
    Integer key = keySetIterator.next();
    System.out.println("key: " + key + " value: " + m.get(key));
  }

  //Check if map contains specified key.
  System.out.println("Map has key 7 ? : "+m.containsKey(7));

  //Check if map contains specified value.
  System.out.println("Map has value twenty ? : "+m.containsValue("Ahmedabad"));
 }
}

Output :
Map : {1=Tokyo, 2=Paris, 3=Chicago, 5=Delhi, 7=Mumbai, 8=Beijing, 11=Berlin}
Map size is : 7
Map is empty? : false
Map value after removing mapping of key 3 : {1=Tokyo, 2=Paris, 5=Delhi, 7=Mumbai, 8=Beijing, 11=Berlin}
Value for key 5 is : Delhi
key: 1 value: Tokyo
key: 2 value: Paris
key: 5 value: Delhi
key: 7 value: Mumbai
key: 8 value: Beijing
key: 11 value: Berlin
Map has key 7 ? : true
Map has value twenty ? : false

No comments:

Post a Comment