Hello friends, we are here today again on our journey to Java. I hope everyone is fine and ready to board our train of knowledge. Today we are gonna learn something very interesting and very exciting, yes I am talking about the HashMap class in Java which is the implementation of hash table data structure. In the past, I have shared my thoughts on How HashMap works in Java as well as frequently asked HashMap interview questions but I have never shared an example of how to use HashMap in Java, or when to use HashMap. Today's topic will definitely be very useful in coding and programming. This topic would surely decrease your time complexity if any task very significantly :p
So what's the wait? Let's start!
1. HashMap class in Java
2. HashMap Declaration:
It takes two parameters namely as follows:
- The type of keys maintained by this map
- The type of mapped values
Now, let's see a working code of HashMap and take the example of our scenario discussed above.
3. How to use HashMap in Java - Code Example
Here is our complete code example to demonstrate how to use the HashMap class in Java. As I said it's an implementation of hash table data structure and you can use HashMap whenever you need a hash table in Java.
import java.util.HashMap;
import java.util.Map;
public class MyMap {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("Student1", 1);
map.put("Tom Cruise", 3);
map.put("Bond, James Bond", 7);
map.put("Selmon Bhoi", 0);
map.put(null, 9);
System.out.println("traversing map");
map.forEach((key, value) -> System.out.println(key + " --> " + value));
System.out.println("getting values from map now...");
System.out.println(map.get("Selmon Bhoi"));
System.out.println(map.get("Tom Cruise"));
System.out.println(map.get(null));
}
}
Output:
HashMap internally calculates the 'hash' of the key and stores value corresponding to a key on the basis of that 'hash'.
4. Important Points about HashMap in Java
- The value of a Java HashMap is determined by the key.
- Only unique keys are stored in the Java HashMap.
- One null key and several null values are possible in a Java HashMap.
- HashMap in Java is not synchronized.
- HashMap in Java does not keep track of the order.
- The capacity of the Java HashMap class is 16 by default, with a load factor of 0.75.
Related Java HashMap tutorials you may like
- How does get() method of HashMap work in Java? (answer)
- HashMap vs Hashtable in Java? (answer)
- ArrayList vs HashMap in Java? (answer)
- HashSet vs HashMap in Java? (answer)
- Difference between ConcurrentHashMap and HashMap in Java? (answer)
- How HashSet internally works in Java? (answer)
- How ConcurrentHashMap internally works in Java? (answer)
- HashMap vs LinkedHashMap in Java? (answer)
- The best way to iterate over HashMap in Java? (answer)
- How to sort the HashMap on keys and values in Java? (solution)
- 3 ways to loop over a Map in Java? (example)
- HashMap vs ConcurrentHashMap in Java? (answer)
- Difference between ArrayList and HashMap? (difference)
- How to convert Map to List in Java? (solution)
Thanks for reading this article so far. If you like this Java HashMap Tutorial and example then please share with your friends and colleages. If you have any questions or feedback, please ask in comments.P. S. - If you are new to Java and want to learn core Java in depth, you can also checkout these 10 Free Core Java Courses to staart with. It contains best free Java courses from Udemy and Coursera for beginners.
No comments:
Post a Comment