Hash Map
Hash Map
OFF
Cyber Monday: Get all Java Courses for Lifetime – yours forever. Claim Your
Discount.
Sale ends in 0d : 1hrs : 37mins : 29s
Programiz
Courses
Tutorials
Examples
The HashMap class of the Java collections framework provides the functionality of
the hash table data structure.
It stores elements in key/value pairs. Here, keys are unique identifiers used to
associate each value on a map.
class Main {
public static void main(String[] args) {
// create a hashmap
HashMap<String, Integer> languages = new HashMap<>();
Here, we have used the put() method to add elements to the hashmap. We will learn
more about the put() method later in this tutorial.
Add elements
Access elements
Change elements
Remove elements
1. Add elements to a HashMap
To add a single element to the hashmap, we use the put() method of the HashMap
class. For example,
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// create a hashmap
HashMap<String, Integer> numbers = new HashMap<>();
System.out.println("Initial HashMap: " + numbers);
// put() method to add elements
numbers.put("One", 1);
numbers.put("Two", 2);
numbers.put("Three", 3);
System.out.println("HashMap after put(): " + numbers);
}
}
Run Code
Output
Initial HashMap: {}
HashMap after put(): {One=1, Two=2, Three=3}
In the above example, we have created a HashMap named numbers. Here, we have used
the put() method to add elements to numbers.
numbers.put("One", 1);
Here, we are passing the String value One as the key and Integer value 1 as the
value to the put() method.
Recommended Readings
import java.util.HashMap;
class Main {
public static void main(String[] args) {
languages.get(1);
Here, the get() method takes the key as its argument and returns the corresponding
value associated with the key.
We can also access the keys, values, and key/value pairs of the hashmap as set
views using keySet(), values(), and entrySet() methods respectively. For example,
import java.util.HashMap;
class Main {
public static void main(String[] args) {
HashMap<Integer, String> languages = new HashMap<>();
languages.put(1, "Java");
languages.put(2, "Python");
languages.put(3, "JavaScript");
System.out.println("HashMap: " + languages);
Recommended Readings
import java.util.HashMap;
class Main {
public static void main(String[] args) {
languages.replace(2, "C++");
Here, we are changing the value referred to by key 2 with the new value C++.
The HashMap class also provides some variations of the replace() method. To learn
more, visit
import java.util.HashMap;
class Main {
public static void main(String[] args) {
We can also remove the entry only under certain conditions. For example,
remove(2, "C++");
Here, the remove() method only removes the entry if the key 2 is associated with
the value C++. Since 2 is not associated with C++, it doesn't remove the entry.
import java.util.HashMap;
import java.util.Map.Entry;
class Main {
public static void main(String[] args) {
// create a HashMap
HashMap<Integer, String> languages = new HashMap<>();
languages.put(1, "Java");
languages.put(2, "Python");
languages.put(3, "JavaScript");
System.out.println("HashMap: " + languages);
import java.util.HashMap;
import java.util.TreeMap;
class Main {
public static void main(String[] args) {
// create a treemap
TreeMap<String, Integer> evenNumbers = new TreeMap<>();
evenNumbers.put("Two", 2);
evenNumbers.put("Four", 4);
System.out.println("TreeMap: " + evenNumbers);
Note: While creating a hashmap, we can include optional parameters: capacity and
load factor. For example,
Previous Tutorial:
Java Map Interface
Next Tutorial:
Java LinkedHashMap
Share on:
Did you find this article helpful?
Related Tutorials
Java Library
Java HashMap putIfAbsent()
Java Library
Java Library
Java Library
Tutorials
Python 3 Tutorial
JavaScript Tutorial
SQL Tutorial
C Tutorial
Java Tutorial
Kotlin Tutorial
C++ Tutorial
Swift Tutorial
C# Tutorial
Go Tutorial
DSA Tutorial
Examples
Python Examples
JavaScript Examples
C Examples
Java Examples
Kotlin Examples
C++ Examples
Company
About
Advertising
Privacy Policy
Terms & Conditions
Contact
Blog
Youtube
Apps
Learn Python
Learn C Programming
Learn Java
© Parewa Labs Pvt. Ltd. All rights reserved.