Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
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.
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 mainly depends on 2 parameters:
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.
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.
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 21st December,2024 21st December SAT&SUN (Weekend Batch) | View Details |
Java Course Online | Class Starts on 1st March,2025 1st March SAT&SUN (Weekend Batch) | View Details |
edureka.co