Ticker

6/recent/ticker-posts

HashMap in Java

HashMap in Java

Introduction
HashMap is a data structure in Java that stores elements in key-value pairs. It is a part of the Java Collections Framework and is implemented in the java.util package. HashMap allows quick retrieval and insertion of elements, making it efficient for handling large datasets. It uses a hashing technique to store and organize data.

Features of HashMap:

  1. Key-Value Pairs: HashMap stores data as key-value pairs, where each key is unique.
  2. Fast Access: Retrieving elements from HashMap is fast because it uses hashing to index and access data.
  3. No Order: HashMap does not maintain any order of elements, and the order may change during iteration.
  4. Null Values and Keys: HashMap allows null values and a single null key.
  5. Synchronized and Unsynchronized Versions: Java provides both synchronized and unsynchronized versions of HashMap.
  6. Initial Capacity and Load Factor: You can specify the initial capacity and load factor for better performance.

Example: Creating and Using a HashMap
Let's see an example of how to create and use a HashMap in Java:

java
import java.util.HashMap;

public class HashMapExample {
public static void main(String[] args) {
// Creating a HashMap with Integer keys and String values
HashMap<Integer, String> studentMap = new HashMap<>();

// Adding elements to the HashMap
studentMap.put(101, "John Doe");
studentMap.put(102, "Jane Smith");
studentMap.put(103, "Bob Johnson");

// Accessing elements from the HashMap
String studentName = studentMap.get(102);
System.out.println("Student with roll number 102: " + studentName);

// Removing an element from the HashMap
studentMap.remove(101);

// Checking if a key exists in the HashMap
int rollNumber = 103;
if (studentMap.containsKey(rollNumber)) {
System.out.println("Student with roll number " + rollNumber + " exists.");
} else {
System.out.println("Student with roll number " + rollNumber + " does not exist.");
}

// Iterating through the HashMap
for (Integer roll : studentMap.keySet()) {
String name = studentMap.get(roll);
System.out.println("Roll Number: " + roll + ", Name: " + name);
}

// Size of the HashMap
int size = studentMap.size();
System.out.println("Size of the HashMap: " + size);
}
}

Explanation:

  1. We import the HashMap class from the java.util package.
  2. We create a HashMap instance called studentMap with Integer keys and String values.
  3. We add elements to the HashMap using the put() method, where the first argument is the key and the second argument is the value.
  4. We retrieve a value from the HashMap using the get() method, providing the key as an argument.
  5. We remove an element from the HashMap using the remove() method, passing the key to be removed.
  6. We check if a key exists in the HashMap using the containsKey() method.
  7. We iterate through the HashMap using a for-each loop and keySet().
  8. We find the size of the HashMap using the size() method.

This documentation provides an overview of HashMap in Java, its features, and a code example demonstrating its basic usage. Remember that HashMap is not synchronized by default, so it is recommended to use java.util.concurrent.ConcurrentHashMap if thread safety is required.

Post a Comment

0 Comments