
In Java, hashmaps are important data structures that can make a variety of tasks more efficient. When used, they allow a programmer to relate values in key-value pairs, where one element is used as anindex,and another is retrievedusingthat index.
Here, we will go over what a hashmap is, how it can be used, and some important methods. Let s dive in!
What is a Hashmap in Java?

History-Computer.com
To put it simply, a hash map is a data structure that maps keys to values. It sounds like nothing special, but the way that it does this is very efficient, making it a great solution even for large datasets.
Internally, an array is used, and values are hashed, turning them into an index in the array. The hash function essentially turns a longer string into a shorter one, through a process known as hashing. That being said, these nitty gritty details are unimportant to how you actually use them.
To use a hashmap in Java, you just need to call the HashMap class. Let s go over how to do that.
How Do You Use a Hashmap in Java?
If you want to use a hashmap in a program, you must import it at the top of the Java file. You can do this by typing:
import java.util.HashMap;
Suppose we want to relate a person s name with their age. Let s make a hashmap withStringsas keys andIntegersas the values:
HashMap<String, Integer> ages = new HashMap<>();
If you haven t seen it before, the syntax using angle brackets is known as generics in Java. Essentially, it allows you to specify the types of data used for the keys and values, but they are common in many other places.
By doing this, data does not have to be casted to be used. Note that primitive types cannot be used here, so we use Integer instead of int.
Now, let s add a value to the hashmap. This can be done using theputmethod:
ages.put("Bob", 30);
If we want to retrieve the value later on, we can use thegetmethod:
int bobAge = ages.get("Bob");
There are a couple other things we can do. We can check if a key exists in a hashmap by usingcontainsKey:
boolean hasBob = ages.containsKey("Bob");
And we can remove a key by usingremove:
ages.remove("Bob");
Important Methods
HashCode
Any class can be specified for the keys of a hashmap in Java, and as aforementioned, they are reduced to an index in an array based on their hash. However, you can specify how that hash is calculated. As an example, here s a class that represents a person:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
If we want to manually calculate the hash for a person object, here s how we can do it:
@Override
public int hashCode() {
int result = name.hashCode() + age;
return result;
}
Note that sincehashCodeandequalsare methods ofObject, which is automatically the superclass forPerson, it is best to use@Override. To calculate the hash, we use the hash of the name, which is a string and already has an implementation for it, and we simply add the age of the person.
Equals
While this does stray a bit from hashmaps, it is important to talk about theequalsmethod if we mentionhashCode. Often, if we want to compare two values, we simply use the == operator. However, if you do this with two objects, this simply compares their memory addresses, which is often not what we want.
Instead, we can implement the class sequalsmethod. This defines how to compare two objects of the same class. Here s how we can do this with thePersonclass:
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Person person = (Person) obj;
return age == person.age && name.equals(person.name);
}
At the beginning, we cover some edge cases. If the objects have the same memory address, then they are obviously equal. However, if the given object is null, or they are not of the same class, they are not equal. After this, we simply check if the ages and names are the same, and we returntrueif so.
Why are we talking about this? Earlier, we talked about overriding thehashCodemethod, and if you are going to do so, you should always implement theequalsmethod as well, as they are closely connected.
Hashmap in Java: Wrapping Up
Although not always necessary, hashmaps are fundamental structures, and they can greatly reduce the complexity of a program. There is much more you can learn about them, and doing so will improve your skills as a Java developer.