How do you create a HashMap in Java?

Table of Contents

Introduction

A HashMap is one of the most commonly used classes in Java for storing and manipulating key-value pairs. It is part of the Java Collections Framework and implements the **Map** interface. A HashMap provides an efficient way to store and retrieve data using unique keys. The key-value pair structure allows you to map a specific key to a value, enabling fast lookups and data management.

This article will explain how to create and use a HashMap in Java with practical examples.

Creating a HashMap in Java

In Java, you can create a HashMap by instantiating the HashMap class. The HashMap constructor requires two arguments: the type of the key and the type of the value.

Basic Syntax for Creating a HashMap

  • KeyType: The data type of the key (e.g., String, Integer).
  • ValueType: The data type of the value associated with the key (e.g., String, Double).

Example 1: Creating a Simple HashMap

Output:

Explanation:

  • The HashMap is created with **String** as the key type and **Integer** as the value type.
  • The put() method is used to insert key-value pairs into the map.
  • The HashMap does not guarantee any order of elements.

Example 2: Creating a HashMap with Different Key and Value Types

You can also use different data types for keys and values. For example, using Integer as the key and String as the value:

Output:

Example 3: HashMap with null Keys and Values

A HashMap allows a null key and null values. However, only one null key can be used because keys are unique.

Output:

Key Methods of a HashMap

Here are some of the most commonly used methods in the HashMap class:

1. put(K key, V value)

Inserts a key-value pair into the HashMap.

Example:

2. get(Object key)

Retrieves the value associated with the given key. If the key does not exist, it returns null.

Example:

3. remove(Object key)

Removes the key-value pair for the specified key.

Example:

4. containsKey(Object key)

Checks if a specific key is present in the map.

Example:

5. size()

Returns the number of key-value pairs in the map.

Example:

6. keySet()

Returns a Set of all keys in the map.

Example:

7. values()

Returns a Collection of all values in the map.

Example:

8. clear()

Removes all key-value pairs from the map.

Example:

Practical Example: Employee Information

Let’s create a HashMap to store employee names and their corresponding salaries:

Output:

Conclusion

A HashMap is a versatile and efficient data structure in Java, used for storing key-value pairs. Creating a HashMap is simple and allows you to manage and manipulate data efficiently with methods like put(), get(), and remove(). Depending on your specific requirements (e.g., sorting, ordering), you can choose from other Map implementations such as TreeMap or LinkedHashMap. Understanding how to use a HashMap is essential for many Java programming tasks, especially when you need fast and easy access to data based on keys.

Similar Questions