Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Java HashMap – Know How to Implement HashMap in Java

Last updated on Jul 26,2023 9.1K Views

A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.
10 / 22 Blog from Java Collections

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.

Key value pairs - Java HashMap - EdurekaIt’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

  • Hash Map is a part of a util package in Java.

  • HashMap extends an abstract class AbstractMap which also provides an incomplete implementation of Map interface.

  • It also implements Cloneable and Serializable K and V in the above definition represent Key and Value respectively.

  • HashMap doesn’t allow duplicate keys but allows duplicate values. That means A single key can’t contain more than 1 value but more than 1 key can contain a single value.

  • HashMap allows only null key but multiple null values can be used.

  • This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. It is roughly similar to Hash Table but is unsynchronized.

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 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Java HashMap – Know How to Implement HashMap in Java

edureka.co