Friday, February 19, 2016

Java Hashmap Class

| Friday, February 19, 2016
  • Is is pronounced every bit HashMap because It is hash tabular array based implementation of map interface.
  • It extends AbstractMap shape together with implements Map interface.
  • It allows to shop null fundamental together with null values.
  • It is non synchronized implementation hence if multiple threads accessing it concurrently together with whatever i of them modifies it together with then it must live on synchronized externally.
  • It does non guarantee of maintaining the map order.
Important methods of HashMap Class are every bit below.
  • void clear() : It volition take away together with clear all mapping entries from map.
  • Object clone() : It volition supply you lot shallow re-create of this map.
  • boolean containsKey(Object key) : It volition depository fiscal establishment tally together with render truthful if specified fundamental has whatever mapping inwards this map.
  • boolean containsValue(Object value) : It volition depository fiscal establishment tally together with render truthful if whatever fundamental has mapping alongside given value.
  • Set<Map.Entry<K,V>> entrySet() : It volition render the gear upwards thought of mapping.
  • V get(Object key) : teach method is used for retrieving value of specified fundamental from this map. If no mapping found, It volition render null.
  • boolean isEmpty() : It volition depository fiscal establishment tally together with render truthful if map is empty.
  • Set<K> keySet() : It volition render gear upwards thought of the keys of this map.
  • V put(K key, V value) : It volition associate specified fundamental value mapping inwards this map.
  • void putAll(Map<? extends K,? extends V> m) : It volition re-create all key-value mapping yoke of specified map to this map.
  • V remove(Object key) : It volition take away mapping of specified fundamental from this map.
  • int size() :It volition render size of this map.
  • Collection<V> values() : It volition render collection thought of map values.

HashMapExample :
package JAVAExamples;  import java.util.HashMap; import java.util.Iterator; import java.util.Map;  world shape HashMapExample {   world static void main(String[] args) {   // practise Map   Map<Integer, String> k = novel HashMap<Integer, String>();   // Add fundamental alongside value inwards 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 later removing mapping of fundamental three : "+m);      //Get value from map for specified key.   System.out.println("Value for fundamental v is : "+m.get(5));      //Iterating over map.   Iterator<Integer> keySetIterator = m.keySet().iterator();   while(keySetIterator.hasNext()){     Integer fundamental = keySetIterator.next();     System.out.println("key: " + fundamental + " value: " + m.get(key));   }    //Check if map contains specified key.   System.out.println("Map has fundamental vii ? : "+m.containsKey(7));    //Check if map contains specified value.   System.out.println("Map has value xx ? : "+m.containsValue("Ahmedabad"));  } }

Output :
Map : {1=Tokyo, 2=Paris, 3=Chicago, 5=Delhi, 7=Mumbai, 8=Beijing, 11=Berlin} Map size is : vii Map is empty? : fake Map value later removing mapping of fundamental three : {1=Tokyo, 2=Paris, 5=Delhi, 7=Mumbai, 8=Beijing, 11=Berlin} Value for fundamental v is : Delhi key: 1 value: Tokyo key: ii value: Paris key: v value: Delhi key: vii value: Bombay key: 8 value: Beijing key: eleven value: Berlin Map has fundamental vii ? : truthful Map has value xx ? : false

Related Posts