DEV Community

Nanthini Ammu
Nanthini Ammu

Posted on

HashMap Java

What is HashMap?

  • It stores data in Key-Value pairs.
  • It is part if java.util package.
  • It implements Map interface.
  • Instead of accessing elements by index, use key to retrive the associated value.
  • Keys are values in the HashMap are actually objects.
  • To use primitive data types, we use equivalent wrapper class.
  • If the same key is added more than once, the latest value will overwrite the previous one.
  • Same value can be added more than once with different key values.
  • one null key allowed, multiple null values allowed.

Example :

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

public class Test {

    public static void main(String[] args) {

        HashMap<Integer, String> obj = new HashMap<>();
        obj.put(101,"Tamil");
        obj.put(102,"English");
        obj.put(103,"Maths");
        obj.put(104,"Science");
        obj.put(105,"Social");
        obj.put(106,"Social");
        System.out.println(obj);
}
}

Output:
{101=Tamil, 102=English, 103=Maths, 104=Science, 105=Social, 106=Social}
Enter fullscreen mode Exit fullscreen mode

Iterating HashMap :

Using keySet() to get keys:

for(int i: obj.keySet())
System.out.println(i);

for(int i: obj.keySet())
System.out.println(i + " " + obj.get(i) );

Output :
101 102 103 104 105 106 
101 Tamil
102 English
103 Maths
104 Science
105 Social
106 Social
Enter fullscreen mode Exit fullscreen mode

Using values() to get Values:

for(int i: obj.keySet())
System.out.println(i + " " + obj.get(i) );

Output :
Tamil English Maths Science Social Social
Enter fullscreen mode Exit fullscreen mode

Using forEach() :

obj.forEach((key, value) ->
  System.out.println(key + " -> " + value)
);

Output :
101 -> Tamil
102 -> English
103 -> Maths
104 -> Science
105 -> Social
106 -> Social

Enter fullscreen mode Exit fullscreen mode

Using entrySet() :

for(HashMap.Entry<Integer,String> ent: obj.entrySet())
System.out.println(ent.getKey()+"->"+ent.getValue());

Output :
101->Tamil
102->English
103->Maths
104->Science
105->Social
106->Social
Enter fullscreen mode Exit fullscreen mode

Important Methods :

put() — Insert Key Pair Value
obj.put(101,"Tamil");
get() - Get value using key
obj.get(101);
getOrDefault() - Returns value if key exists, otherwise returns default value.
obj.getOrDefault(107,"Not Found");
remove() - Remove key value pair
obj.remove(101);
obj.remove(101,"Tamil"); - remove only if both key and value match.
containsKey() - Check if key exist.
obj.containsKey(102);
containsValue() - Check if value exist.
obj.containsValue("English");
size() - Returns number if entries.
obj.size();
isEmpty() - Check if map is empty.
obj.isEmpty();
clear() - Remove all data.
obj.clear();
keySet() - Return all keys, used in iteration.
obj.keySet();
values() - Return all values, used in iteration.
obj.values();
entrySet() - Return key value pair, used in iteration.
obj.entrySet();
putIfAbsent() - Adds value only if key is not already present.
obj.putIfAbsent(107,"Hindi");
replace() - Replace value of existing key.
obj.replace(103,"Sports");
forEach() - (Java 8) - To iterate.
obj.forEach((key,value) ->
System.out.println(key + " -> " + value)
);

Top comments (0)