Java HashMap class
Java HashMap class implements the map interface by using a hash table. It inherits AbstractMap class and
implements Map interface.
Points to remember
o Java HashMap class contains values based on the key.
o Java HashMap class contains only unique keys.
o Java HashMap class may have one null key and multiple null values.
o Java HashMap class is non synchronized.
o Java HashMap class maintains no order.
o The initial default capacity of Java HashMap class is 16 with a load factor of 0.75.
Java HashMap example to add() elements
Here, we see different ways to insert elements.
1. import java.util.*;
2. class HashMap1{
3. public static void main(String args[]){
4. HashMap<Integer,String> hm=new HashMap<Integer,String>();
5. System.out.println("Initial list of elements: "+hm);
6. hm.put(100,"Amit");
7. hm.put(101,"Vijay");
8. hm.put(102,"Rahul");
9.
10. System.out.println("After invoking put() method ");
11. for(Map.Entry m:hm.entrySet()){
12. System.out.println(m.getKey()+" "+m.getValue());
13. }
14.
15. hm.putIfAbsent(103, "Gaurav");
16. System.out.println("After invoking putIfAbsent() method ");
17. for(Map.Entry m:hm.entrySet()){
18. System.out.println(m.getKey()+" "+m.getValue());
19. }
20. HashMap<Integer,String> map=new HashMap<Integer,String>();
21. map.put(104,"Ravi");
22. map.putAll(hm);
23. System.out.println("After invoking putAll() method ");
24. for(Map.Entry m:map.entrySet()){
25. System.out.println(m.getKey()+" "+m.getValue());
26. }
27. }
28. }
Initial list of elements: {}
After invoking put() method
100 Amit
101 Vijay
102 Rahul
After invoking putIfAbsent() method
100 Amit
101 Vijay
102 Rahul
103 Gaurav
After invoking putAll() method
100 Amit
101 Vijay
102 Rahul
103 Gaurav
104 Ravi
Java HashMap example to remove() elements
Here, we see different ways to remove elements.
1. import java.util.*;
2. public class HashMap2 {
3. public static void main(String args[]) {
4. HashMap<Integer,String> map=new HashMap<Integer,String>();
5. map.put(100,"Amit");
6. map.put(101,"Vijay");
7. map.put(102,"Rahul");
8. map.put(103, "Gaurav");
9. System.out.println("Initial list of elements: "+map);
10. //key-based removal
11. map.remove(100);
12. System.out.println("Updated list of elements: "+map);
13. //value-based removal
14. map.remove(101);
15. System.out.println("Updated list of elements: "+map);
16. //key-value pair based removal
17. map.remove(102, "Rahul");
18. System.out.println("Updated list of elements: "+map);
19. }
20. }
Output:
Initial list of elements: {100=Amit, 101=Vijay, 102=Rahul, 103=Gaurav}
Updated list of elements: {101=Vijay, 102=Rahul, 103=Gaurav}
Updated list of elements: {102=Rahul, 103=Gaurav}
Updated list of elements: {103=Gaurav}
Java HashMap example to replace() elements
Here, we see different ways to replace elements.
1. import java.util.*;
2. class HashMap3{
3. public static void main(String args[]){
4. HashMap<Integer,String> hm=new HashMap<Integer,String>();
5. hm.put(100,"Amit");
6. hm.put(101,"Vijay");
7. hm.put(102,"Rahul");
8. System.out.println("Initial list of elements:");
9. for(Map.Entry m:hm.entrySet())
10. {
11. System.out.println(m.getKey()+" "+m.getValue());
12. }
13. System.out.println("Updated list of elements:");
14. hm.replace(102, "Gaurav");
15. for(Map.Entry m:hm.entrySet())
16. {
17. System.out.println(m.getKey()+" "+m.getValue());
18. }
19. System.out.println("Updated list of elements:");
20. hm.replace(101, "Vijay", "Ravi");
21. for(Map.Entry m:hm.entrySet())
22. {
23. System.out.println(m.getKey()+" "+m.getValue());
24. }
25. System.out.println("Updated list of elements:");
26. hm.replaceAll((k,v) -> "Ajay");
27. for(Map.Entry m:hm.entrySet())
28. {
29. System.out.println(m.getKey()+" "+m.getValue());
30. }
31. }
32. }
Initial list of elements:
100 Amit
101 Vijay
102 Rahul
Updated list of elements:
100 Amit
101 Vijay
102 Gaurav
Updated list of elements:
100 Amit
101 Ravi
102 Gaurav
Updated list of elements:
100 Ajay
101 Ajay
102 Ajay
Program 2: Mapping Integer Values to String Keys.
// Java code to illustrate the size() method
import java.util.*;
public class Hash_Map_Demo {
public static void main(String[] args)
{
// Creating an empty HashMap
HashMap<String, Integer> hash_map = new HashMap<String, Integer>();
// Mapping int values to string keys
hash_map.put("Geeks", 10);
hash_map.put("4", 15);
hash_map.put("Geeks", 20);
hash_map.put("Welcomes", 25);
hash_map.put("You", 30);
// Displaying the HashMap
System.out.println("Initial Mappings are: " + hash_map);
// Displaying the size of the map
System.out.println("The size of the map is " + hash_map.size());
}
}
Initial Mappings are: {4=15, Geeks=20, You=30, Welcomes=25}
The size of the map is 4
HashMap putAll() Method in Java
The java.util.HashMap.putAll() is an inbuilt method of HashMap class that is used for the copy operation. The
method copies all of the elements i.e., the mappings, from one map into another.
// Java code to illustrate the putAll() method
import java.util.*;
public class Hash_Map_Demo {
public static void main(String[] args) {
// Creating an empty HashMap
HashMap<Integer, String> hash_map = new HashMap<Integer, String>();
// Mapping string values to int keys
hash_map.put(10, "Geeks");
hash_map.put(15, "4");
hash_map.put(20, "Geeks");
hash_map.put(25, "Welcomes");
hash_map.put(30, "You");
// Displaying the HashMap
System.out.println("Initial Mappings are: " + hash_map);
// Creating a new hash map and copying
HashMap<Integer, String> new_hash_map = new HashMap<Integer, String>();
new_hash_map.putAll(hash_map);
// Displaying the final HashMap
System.out.println("The new map looks like this: " + new_hash_map);
}
}
Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
The new map looks like this: {25=Welcomes, 10=Geeks, 20=Geeks, 30=You, 15=4}
// Java code to illustrate the putAll() method
import java.util.*;
public class Hash_Map_Demo {
public static void main(String[] args)
{
// Creating an empty HashMap
HashMap<String, Integer> hash_map = new HashMap<String, Integer>();
// Mapping int values to string keys
hash_map.put("Geeks", 10);
hash_map.put("4", 15);
hash_map.put("Geeks", 20);
hash_map.put("Welcomes", 25);
hash_map.put("You", 30);
// Displaying the HashMap
System.out.println("Initial Mappings are: " + hash_map);
// Creating a new hash map and copying
HashMap<String, Integer> new_hash_map = new HashMap<String,
Integer>();
new_hash_map.putAll(hash_map);
// Displaying the final HashMap
System.out.println("The new map looks like this: " + new_hash_map);
}
}
Initial Mappings are: {4=15, Geeks=20, You=30, Welcomes=25}
The new map looks like this: {Geeks=20, 4=15, You=30, Welcomes=25}
HashMap remove() Method in Java
The java.util.HashMap.remove() is an inbuilt method of HashMap class and is used to remove the mapping of
any particular key from the map. It basically removes the values for any particular key in the Map.
// Java code to illustrate the remove() method
import java.util.*;
public class Hash_Map_Demo {
public static void main(String[] args) {
// Creating an empty HashMap
HashMap<Integer, String> hash_map = new HashMap<Integer, String>();
// Mapping string values to int keys
hash_map.put(10, "Geeks");
hash_map.put(15, "4");
hash_map.put(20, "Geeks");
hash_map.put(25, "Welcomes");
hash_map.put(30, "You");
// Displaying the HashMap
System.out.println("Initial Mappings are: " + hash_map);
// Removing the existing key mapping
String returned_value = (String)hash_map.remove(20);
// Verifying the returned value
System.out.println("Returned value is: "+ returned_value);
// Displayin the new map
System.out.println("New map is: "+ hash_map);
}
}
Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
Returned value is: Geeks
New map is: {25=Welcomes, 10=Geeks, 30=You, 15=4}
Program 2: When passing a new key.
// Java code to illustrate the remove() method
import java.util.*;
public class Hash_Map_Demo {
public static void main(String[] args) {
// Creating an empty HashMap
HashMap<Integer, String> hash_map = new HashMap<Integer, String>();
// Mapping string values to int keys
hash_map.put(10, "Geeks");
hash_map.put(15, "4");
hash_map.put(20, "Geeks");
hash_map.put(25, "Welcomes");
hash_map.put(30, "You");
// Displaying the HashMap
System.out.println("Initial Mappings are: " + hash_map);
// Removing the new key mapping
String returned_value = (String)hash_map.remove(50);
// Verifying the returned value
System.out.println("Returned value is: "+ returned_value);
// Displayin the new map
System.out.println("New map is: "+ hash_map);
}
}
Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
Returned value is: null
New map is: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
Difference between HashSet and HashMap
HashSet contains only values whereas HashMap contains an entry(key and value).
Java HashMap Example: Book
1. import java.util.*;
2. class Book {
3. int id;
4. String name,author,publisher;
5. int quantity;
6. public Book(int id, String name, String author, String publisher, int quantity) {
7. this.id = id;
8. this.name = name;
9. this.author = author;
10. this.publisher = publisher;
11. this.quantity = quantity;
12. }
13. }
14. public class MapExample {
15. public static void main(String[] args) {
16. //Creating map of Books
17. Map<Integer,Book> map=new HashMap<Integer,Book>();
18. //Creating Books
19. Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
20. Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
21. Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
22. //Adding Books to map
23. map.put(1,b1);
24. map.put(2,b2);
25. map.put(3,b3);
26.
27. //Traversing map
28. for(Map.Entry<Integer, Book> entry:map.entrySet()){
29. int key=entry.getKey();
30. Book b=entry.getValue();
31. System.out.println(key+" Details:");
32. System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
33. }
34. }
35. }
Output:
1 Details:
101 Let us C Yashwant Kanetkar BPB 8
2 Details:
102 Data Communications & Networking Forouzan Mc Graw Hill 4
3 Details:
103 Operating System Galvin Wiley 6
Difference between HashMap and HashSet
Example of HashSet and HashMap in Java
HashSet
// Java program to demonstrate working of HashSet
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args)
{
HashSet<String> hs = new HashSet<String>();
// Adding elements to the HashSet
hs.add("geeks");
hs.add("practice");
hs.add("contribute");
;
System.out.println("Before adding duplicate values \n\n" + hs);
// Addition of duplicate elements
hs.add("geeks");
hs.add("practice");
System.out.println("\nAfter adding duplicate values \n\n" + hs);
// Addition of null values
hs.add(null);
hs.add(null);
// Displaying HashSet elements
System.out.println("\nAfter adding null values \n\n" + hs);
}
}
Output:
Before adding duplicate values
[practice, geeks, contribute]
After adding duplicate values
[practice, geeks, contribute]
After adding null values
[null, practice, geeks, contribute]
HashMap
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args)
{
/* This is how to declare HashMap */
HashMap<Integer, String> hm = new HashMap<Integer, String>();
/*Adding elements to HashMap*/
hm.put(12, "geeks");
hm.put(2, "practice");
hm.put(7, "contribute");
System.out.println("\nHashMap object output :\n\n" + hm);
// store data with duplicate key
hm.put(12, "geeks");
System.out.println("\nAfter inserting duplicate key :\n\n" + hm);
}
}
Output:
HashMap object output :
{2=practice, 7=contribute, 12=geeks}
After inserting duplicate key :
{2=practice, 7=contribute, 12=geeks}