What is HashMap in java?
- HashMap is Hash table based implementation of the Map interface.
- HashMap provides constant-time performance for the basic operations (get and put).
- HashMap based implementation is not thread safe.
- HashMap must synchronized externally, If multiple threads access a hashmap concurrently.
List of Important methods of HashMap in java
-
Remove
-
put
-
Sort
-
Iterate
-
Get
-
Contains And
-
Clear
Program – HashMap functions in java
package org.learn.collection.map.hmap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class DemoHashMap {
public static void main(String[] args) {
Map<String, String> mapSportsPersonality = new HashMap<>();
mapSportsPersonality.put("Tennis", "Federer");
mapSportsPersonality.put("Cricket", "Bradman");
mapSportsPersonality.put("Basketball", "Jordan");
mapSportsPersonality.put("Golf", "Woods");
mapSportsPersonality.put("Boxer", "Ali");
System.out.println("1. Demo of remove methods: ");
demoRemoveMethod(mapSportsPersonality);
System.out.println("\n2. Demo of put methods: ");
demoPutMethod(mapSportsPersonality);
System.out.println("\n3. Sort elements of HashMap: ");
demoSortMethod(mapSportsPersonality);
System.out.println("\n4. Iterate through HashMap: ");
demoIterateMethod(mapSportsPersonality);
System.out.println("\n6. Demo of get method of HashMap: ");
System.out.println("Element with key Golf: " + mapSportsPersonality.get("Golf"));
System.out.println("Element with key Tennis: " + mapSportsPersonality.get("Tennis"));
System.out.println("\n7. Demo of contains method of HashMap: ");
System.out.println("Check HashMap contains Boxer: " + mapSportsPersonality.containsKey("Boxer"));
System.out.println("Check HashMap contains soccer: " + mapSportsPersonality.containsKey("soccer"));
System.out.println("\n8.Demo of clear method of HashMap: ");
mapSportsPersonality.clear();
System.out.println("Clear HashMap: " + mapSportsPersonality);
}
private static void demoRemoveMethod(Map<String, String> mapSportsPersonality) {
// {Tennis=Federer, Cricket=Bradman, Golf=Woods, Basketball=Jordan,
// Boxer=Ali}
System.out.println("1.1. Orignal hashmap:\n" + mapSportsPersonality);
// Remove Cricket from map
mapSportsPersonality.remove("Cricket");
// {Tennis=Federer, Golf=Woods, Basketball=Jordan, Boxer=Ali}
System.out.println("1.2. Removed Cricket from map : \n" + mapSportsPersonality);
mapSportsPersonality.remove("Boxer","Ali");
// {Tennis=Federer, Golf=Woods, Basketball=Jordan}
System.out.println("1.3. Removed entry from map with Key=Boxer and Value=Ali :\n " + mapSportsPersonality);
Set<Entry<String, String>> entrySet = mapSportsPersonality.entrySet();
Iterator<Entry<String, String>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
if (entry.getKey().equals("Basketball")) {
iterator.remove();
} else if (entry.getKey().equals("Golf")) {
iterator.remove();
}
}
// {Tennis=Federer}
System.out.println("1.4. Removed Basketball,Golf during iteration:\n " + mapSportsPersonality);
}
private static void demoPutMethod(Map<String, String> mapSportsPersonality) {
System.out.println("2.1. Orignal HashMap:\n" + mapSportsPersonality);
mapSportsPersonality.put("Tennis", "Federer");
mapSportsPersonality.put("Cricket", "Bradman");
System.out.println("2.2. Added Tennis & Cricket using put: \n" + mapSportsPersonality);
Map<String, String> newSportsMap = new HashMap<>();
newSportsMap.put("Golf", "Woods");
newSportsMap.put("Boxer", "Ali");
mapSportsPersonality.putAll(newSportsMap);
System.out.println("2.3. Added another map using putAll to map:\n" + mapSportsPersonality);
mapSportsPersonality.putIfAbsent("Baseball", "Trout");
// key Baseball already added to map, so "Kershaw" would not be added
mapSportsPersonality.putIfAbsent("Baseball", "Kershaw");
System.out.println("2.4. Added Baseball using putIfAbsent to map:\n" + mapSportsPersonality);
}
private static void demoSortMethod(Map<String, String> mapSportsPersonality) {
// {Tennis=Federer, Cricket=Bradman, Golf=Woods, Baseball=Trout, Boxer=Ali}
System.out.println("Orignal HashMap:" + mapSportsPersonality);
System.out.println("\n3.1. Sort HashMap by ascending key: ");
TreeMap<String, String> mapSorted = new TreeMap<>(mapSportsPersonality);
mapSorted.forEach((key, value) -> {
System.out.println(key + ", " + value);
});
System.out.println("\n3.2. Sort HashMap by descending key: ");
mapSorted.descendingMap().forEach((key, value) -> {
System.out.println(key + ", " + value);
});
}
private static void demoIterateMethod(Map<String, String> mapSportsPersonality) {
System.out.println("4.1. Iterate using forEach:");
// Output using Java 8
mapSportsPersonality.forEach((key, value) -> {
System.out.println("Game:" + key + ", Player:" + value);
});
System.out.println("\n4.2. Iterate using iterator:");
// Output use Iterator:
Set<Entry<String, String>> entrySet = mapSportsPersonality.entrySet();
Iterator<Entry<String, String>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
System.out.println("Game:" + entry.getKey() + ", Player:" + entry.getValue());
}
}
}
Output – Various method of HashMap in java
1. Demo of remove methods:
1.1. Orignal hashmap:
{Tennis=Federer, Cricket=Bradman, Golf=Woods, Basketball=Jordan, Boxer=Ali}
1.2. Removed Cricket from map :
{Tennis=Federer, Golf=Woods, Basketball=Jordan, Boxer=Ali}
1.3. Removed entry from map with Key=Boxer and Value=Ali :
{Tennis=Federer, Golf=Woods, Basketball=Jordan}
1.4. Removed Basketball,Golf during iteration:
{Tennis=Federer}
2. Demo of put methods:
2.1. Orignal HashMap:
{Tennis=Federer}
2.2. Added Tennis & Cricket using put:
{Tennis=Federer, Cricket=Bradman}
2.3. Added another map using putAll to map:
{Tennis=Federer, Cricket=Bradman, Golf=Woods, Boxer=Ali}
2.4. Added Baseball using putIfAbsent to map:
{Tennis=Federer, Cricket=Bradman, Golf=Woods, Baseball=Trout, Boxer=Ali}
3. Sort elements of HashMap:
Orignal HashMap:{Tennis=Federer, Cricket=Bradman, Golf=Woods, Baseball=Trout, Boxer=Ali}
3.1. Sort HashMap by ascending key:
Baseball, Trout
Boxer, Ali
Cricket, Bradman
Golf, Woods
Tennis, Federer
3.2. Sort HashMap by descending key:
Tennis, Federer
Golf, Woods
Cricket, Bradman
Boxer, Ali
Baseball, Trout
4. Iterate through HashMap:
4.1. Iterate using forEach:
Game:Tennis, Player:Federer
Game:Cricket, Player:Bradman
Game:Golf, Player:Woods
Game:Baseball, Player:Trout
Game:Boxer, Player:Ali
4.2. Iterate using iterator:
Game:Tennis, Player:Federer
Game:Cricket, Player:Bradman
Game:Golf, Player:Woods
Game:Baseball, Player:Trout
Game:Boxer, Player:Ali
6. Demo of get method of HashMap:
Element with key Golf: Woods
Element with key Tennis: Federer
7. Demo of contains method of HashMap:
Check HashMap contains Boxer: true
Check HashMap contains soccer: false
8.Demo of clear method of HashMap:
Clear HashMap: {}