A Brief Introduction of HashMap in Java
HashMap in Java is a class that implements a map interface in the collection framework. In this article, we will briefly cover how to declare and define HashMap in Java using examples.
HashMap in Java is use to store the scenarios where we want to store the objects in key value pairs. In this article, we will learn HashMap in Java with the help of examples. The article, will also covers the creation and operation on HashMap.
Must Check: What is Java?
Must Check: Top Java Online Courses & Certifications
Now, without further delay letโs dive deep to know more about HashMap in Java.
Table of Content
Best-suited Java courses for you
Learn Java with these high-rated online courses
What is HashMap in Java?
If we want to represent a group of objects as key-value pairs then we should go for Map and HashMap is a class that implements a map interface in the collection framework in Java.
For example:
Student_roll_no | Student_name |
100 | Inder |
101 | Kamlesh |
102 | Vikram |
103 | Shubham |
In the above example, Student_roll_no is considered as a key and Student_name is considered as the value of the respective key.
Also Read: Difference between HashMap and HashTable
How to create HashMap in Java?
Before creating and using a HashMap, we first have to import java.util.HashMap package.
HashMap<Type,Type> map = new HashMap<>();
Now, letโs see how to store elements in HashMap.
import java.util.HashMap;public class Main{ public static void main(String[] args) throws Exception { // create HashMap object HashMap<Integer,String> student=new HashMap<>();
// Insert key and respective value into HashMap using put() map.put(100,"Inder"); map.put(101,"Kamlesh"); map.put(102,"Vikram"); map.put(103,"Shubham"); }}
Here, the student is a Reference of the HashMap object, and with this Reference, we are calling the HashMap method put () to store student_roll_no as key and student_name as value.
Methods in HashMap
Methods | Description |
put() | Add element is HashMap |
get() | To get value using key |
remove() | To remove element using key |
replace() | To replace value in specific key |
size() | Return size of the HashMap |
isEmpty() | Checks if HashMap is empty() |
containsKey() | Checks if specified key is present in HashMap |
containsValue() | Checks if specified value is present in HashMap |
Also Read: Introduction to MapReduce
Operations on HashMap
We can perform different operations on HashMap, such as:
- Add Element
- Access Element
- Change Element
- Remove Element
- Iterate Through a HashMap
Now, letโs discuss one by one with the help of examples:
Adding Element in HashMap
Example: Add Employee Code and Employee name in the HashMap as a key and value respectively.
import java.util.HashMap;
public class Main{ public static void main(String[] args) throws Exception {
// create HashMap object
HashMap<Integer,String> employee=new HashMap<>();
// Insert key and respective value into HashMap using put()
employee.put(1001,"Ravi"); employee.put(1002,"Virat"); employee.put(1003,"Shiv"); employee.put(1004,"Vikram"); employee.put(1005,"Sumit");
System.out.println(employee); }}
Output:
Access Element from HashMap
Example: Access employee name from HashMap of employee code 1002.
import java.util.HashMap;public class Main{ public static void main(String[] args) throws Exception {
// create HashMap object
HashMap<Integer,String> employee=new HashMap<>();
// Insert key and respective value into HashMap using put()
employee.put(1001,"Ravi"); employee.put(1002,"Virat"); employee.put(1003,"Shiv"); employee.put(1004,"Vikram"); employee.put(1005,"Sumit");
// Access employee name from HashMap using get() method.
String emp_name=employee.get(1002); System.out.println("Employee name having 1002 as emp_code is:"+emp_name); }}
Output
Also Read: Data Structure and Algorithm in Java
Change Element in HashMap
Example: Change the value of key 1002 from Virat to Rohit
import java.util.HashMap;
public class Main{ public static void main(String[] args) throws Exception {
// create HashMap object
HashMap<Integer,String> employee=new HashMap<>();
// Insert key and respective value into HashMap using put()
employee.put(1001,"Ravi"); employee.put(1002,"Virat"); employee.put(1003,"Shiv"); employee.put(1004,"Vikram"); employee.put(1005,"Sumit");
// value of key 1002 is changed to Rohit from Virat
employee .replace(1002,"Rohit");
// Access employee name from HashMap using get() method.
String emp_name=employee.get(1002); System.out.println("Employee name having 1002 as emp_code is:"+emp_name); }}
Output
Remove Element in HashMap
Exmaple: Remove element with key 1002
import java.util.HashMap;
public class Main{ public static void main(String[] args) throws Exception {
// create HashMap object
HashMap<Integer,String> employee=new HashMap<>();
// Insert key and respective value into HashMap using put()
employee.put(1001,"Ravi"); employee.put(1002,"Virat"); employee.put(1003,"Shiv"); employee.put(1004,"Vikram"); employee.put(1005,"Sumit"); //Remove element with key 1002
employee.remove(1002); System.out.println(employee); }}
Output:
Iterate Through a HashMap
Example: Iterates the HashMap through key, value and entrySet.
import java.util.HashMap;import java.util.Map.Entry;
public class Main{ public static void main(String[] args) throws Exception {
// create HashMap object
HashMap<Integer,String> employee=new HashMap<>();
// Insert key and respective value into HashMap using put()
employee.put(1001,"Ravi"); employee.put(1002,"Virat"); employee.put(1003,"Shiv"); employee.put(1004,"Vikram"); employee.put(1005,"Sumit"); //Iterate through Keys
System.out.println("Iterate through Keys:"); for(Integer key: employee.keySet()) { System.out.println(key); } // Iterate through values
System.out.println("Iterate through values:"); for(String value: employee.values()) { System.out.println(value); } //Iterate through entrySet
System.out.println("Iterate through entrySet:"); for(Entry<Integer,String> entry: employee.entrySet()) { System.out.println(entry); } }}
Output:
Conclusion
In this article, we have discussed:
- What is HashMap and How to Use HashMap in Java
- Different methods to performs operation on HashMap.
- Examples
Hope you will like the article.
Contributed By: Shubham Kumar
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio