Tutorials Hut

Tutorials Hut

Java HashMap

Java HashMap<K, V> implements Map interface, it represents Map data structure, each element in the map is a key, value pair.

  • HashMap keys are always unique.
  • HashMap is not thread safe.
  • A key will always correspond to a single value.
  • Key should be immutable.
  • HashMap uses hashing algorithms to uniquely store and find data.
  • HashMap is similar to HashTable but it is not synchronized safe on other had HashTable is synchronized(thread safe).
  • Default initalCapacity of hashMap is 16 and load factor is 0.75.

HashMap Constructors

HashMap has below 4 types of constructors:

1. HashMap() – This constructor has no parameters.

Example:

HashMap myMap = new HashMap();

2.HashMap(int initialCapacity) – HashMap with initial capacity parameter.

Example:

HashMap myMapWithCap = new HashMap(12);

3. HashMap(int initialCapacity, float loadFactor) – HashMap with initial capacity and load factor.

Example:

HashMap myMapWithCapAndLoadFactor = new HashMap(12, .75F);

4. HashMap(Map map) – HashMap which takes another map as parameter.

Example:

HashMap myMap = new HashMap();
HashMap mapWhichTakesAnotherMap = new HashMap(myMap);

HashMap Example Non Generic

In a non-generic or old way, we don’t specify the  data type of key and value.

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

public class MapExamples {
public static void main(String[] args) {

    HashMap myMap = new HashMap();
    myMap.put("101", "Tom");
    myMap.put("102", "Rom");
    myMap.put("103", "Jonny");
    myMap.get("101");
    Set eSet= myMap.entrySet();
    Iterator itr = eSet.iterator();
    while(itr.hasNext()) {
       Map.Entry entry = (Map.Entry)itr.next();
       System.out.println("Key:"+entry.getKey()+" value:"+entry.getValue());
    }
}
}

Output

Key:101 value:Tom
Key:102 value:Rom
Key:103 value:Jonny

HashMap Example Generic

In the generic map we specify the data type of key and value pair.

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

public class MapExamples {
public static void main(String[] args) {

    HashMap <String, String> myMap = new HashMap<String, String>();
    myMap.put("101", "Tom");
    myMap.put("102", "Rom");
    myMap.put("103", "Jonny");
    myMap.get("101");
     Set eSet= myMap.entrySet();

    Iterator itr = eSet.iterator();
    while(itr.hasNext()) {
        Map.Entry entry = (Map.Entry)itr.next();
        System.out.println("Key:"+entry.getKey()+" value:"+entry.getValue());
    }
}
}

Output

Key:101 value:Tom
Key:102 value:Rom
Key:103 value:Jonny

HashMap Add, Remove and Iterate Operations

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

public class HashMapExamples {
public static void main(String[] args) {
    HashMap<String, String> myMap = new HashMap<String, String>();
    myMap.put("101", "Tom"); //Adds element in map
    myMap.put("102", "Rom");
     myMap.put("103", "Jonny");

    //Get a value for given key
    String getValue = myMap.get("101");

    //Remove
    myMap.remove("103");

    Set eSet = myMap.entrySet();
     Iterator itr = eSet.iterator();

    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        System.out.println("Key:" + entry.getKey() + " value:" + entry.getValue());
    }
}
}

Output

Key:101 value:Tom
Key:102 value:Rom

Next Article


















  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    Scroll to Top