Java Intermediate

0% completed

Previous
Next
Basics of HashMap Class

The HashMap class is one of the most commonly used implementations of the Map interface in Java. It stores data as key-value pairs, allowing you to quickly retrieve a value when you know its associated key. HashMap does not maintain any order for its entries, and it allows one null key and multiple null values. Its unsynchronized nature makes it fast in single-threaded scenarios, but if you need thread safety, you must manage it externally.

Syntax for Creating a HashMap

To create a HashMap, you use the following syntax:

HashMap<KeyType, ValueType> map = new HashMap<KeyType, ValueType>();
  • Explanation:
    • HashMap<KeyType, ValueType> declares a HashMap that will store keys of type KeyType and values of type ValueType.
    • The constructor new HashMap<KeyType, ValueType>() initializes an empty HashMap.

Additional constructors allow you to set an initial capacity and load factor:

HashMap<KeyType, ValueType> map = new HashMap<KeyType, ValueType>(initialCapacity); HashMap<KeyType, ValueType> map = new HashMap<KeyType, ValueType>(initialCapacity, loadFactor);
  • Explanation:
    • Initial Capacity: Specifies the number of buckets in the hash table at creation.
    • Load Factor: Determines how full the hash table is allowed to get before its capacity is automatically increased (commonly set to 0.75).

Basic Operations in HashMap

1. Creating a HashMap

Syntax:

HashMap<String, Integer> map = new HashMap<String, Integer>();
  • Explanation:
    • This code creates a new HashMap that uses String objects as keys and Integer objects as values.

2. Inserting Elements into a HashMap

Elements are added using the put() method.

Syntax:

map.put(key, value);
  • Explanation:
    • The put(key, value) method associates the specified value with the specified key in the map.
    • If the map already contains a mapping for the key, the old value is replaced with the new value.

Example: Creating and Inserting Elements into a HashMap

In this example, we create a HashMap to store student names as keys and their corresponding grades as values. We then insert multiple key-value pairs into the map using the put() method.

Java
Java

. . . .

Example Explanation:

  • HashMap Creation:
    • A HashMap<String, Integer> named studentGrades is created to store student names and their grades.
  • Inserting Elements:
    • The put() method is used to add key-value pairs to the map.
    • When the key "Alice" is added a second time with a different value, the original value (85) is replaced by 88.
  • Outcome:
    • The final output displays the student names and their latest grades, ensuring that each key is unique.

The HashMap class in Java is a powerful tool for storing data as key-value pairs. Understanding these basic operations is fundamental to working with maps in Java. HashMap is widely used for fast data retrieval based on keys, and mastering its usage is essential for building efficient Java applications.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next