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?
- Features of Hashmap
- Performance of Java HashMap
- Constructors of HashMap in Java
- HashMap Implementation
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.
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:
- Initial Capacity: Capacity is simply the number of buckets whereas the Initial Capacity is the capacity of HashMap instance when it is created.
- 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:
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.