Java HashMap – Know How to Implement HashMap in Java

Last updated on Jul 26,2023 9.3K Views
A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.

Java HashMap – Know How to Implement HashMap in Java

edureka.co

HashMap is a Map-based collection class in Java which is used to store data in Key & Value pairs. It also helps in implementing the Map interface in Java. Through the medium of this article, I will tell you how to implement Java HashMap.

Below topics are covered in this article:

What is a Java HashMap?

HashMap is basically a part of Java’s collection since Java 1.2. It provides the basic implementation of the Map interface in Java. It generally stores the data in pairs in the form of (Key, Value). To access a value within the HashMap one must know its Key.

It’s named as HashMap because it uses a technique called Hashing. Hashing is a process of converting a larger String to a smaller one by keeping the value of the String as constant. The resulting compressed value helps in indexing and faster searches.

With this, now let’s know the various features of HashMap in Java.

Features of HashMap

Now that you know what is Hashmap and its various features, let’s move further and understand the performance of Java Hashmap.

Performance of Java HashMap

Performance mainly depends on 2 parameters:

  1. Initial Capacity: Capacity is simply the number of buckets whereas the Initial Capacity is the capacity of HashMap instance when it is created.
  2. Load Factor: The Load Factor is a measure that when rehashing should be done. Rehashing is a process of increasing the capacity. In HashMap capacity is multiplied by 2. Load Factor is also a measure in deciding what fraction of the HashMap is allowed to fill before rehashing. When the number of entries in the HashMap increases, the product of current capacity and Load Factor capacity also increases. That implies rehashing is done.

Note: If the initial capacity is kept higher then rehashing will never be done. But by keeping it higher it increases the time complexity of iteration. So it should be chosen very cleverly to increase performance. The expected number of values should be taken into account to set initial capacity. Most generally preferred load factor value is 0.75 which provides a good deal between time and space costs. Load factor’s value varies between 0 and 1.

Constructors in HashMap

HashMap provides four constructors and the access modifier of each of them is public:

ConstructorsDescription
1. HashMap()It is the default constructor which creates an instance of HashMap with initial capacity 16 and load factor 0.75.
2. HashMap(int initial capacity)This is used to create a HashMap instance with specified initial capacity and load factor 0.75
3. HashMap(int initial capacity, float load factor)It creates a HashMap instance with specified initial capacity and specified load factor.
4. HashMap(Map map)It creates an instance of HashMap with the same mappings as a specified map.

With this, now let’s see how to implement HashMap in Java.

HashMap Implementation

Below program illustrates how to implement HashMap in Java.

package Edureka;

//Java program to illustrate
//Java.util.HashMap
import java.util.HashMap;
import java.util.Map;

public class Hashmap{
public static void main(String[] args){
HashMa<String, Integer> map = new HashMap<>();
print(map);
map.put("abc", 10);
map.put("mno", 30);
map.put("xyz", 20);

System.out.println("Size of map is" + map.size());

print(map);
if (map.containsKey("mno"))
{
Integer a = map.get("mno");
System.out.println("value for key \"mno\" is:- " + a);
}

map.clear();
print(map);
}

public static void print(Map<String, Integer> map){
if (map.isEmpty()){
System.out.println("map is empty");
}
else{
System.out.println(map);
}
}
}

On executing the HashMap program, output goes like this:

map is empty
Size of map is:- 3
{abc=10, xyz=20, mno=30}
value for key "abc" is:- 10
map is empty

So this brings us to the end of the Java HashMap article. I hope you found it informative and helped you in understanding the Fundamentals.

Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “Java HashMap ” article and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 11th May,2024

11th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 1st June,2024

1st June

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial