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:
- Key-Value Pairs: HashMap stores data as key-value pairs, where each key is unique.
- Fast Access: Retrieving elements from HashMap is fast because it uses hashing to index and access data.
- No Order: HashMap does not maintain any order of elements, and the order may change during iteration.
- Null Values and Keys: HashMap allows null values and a single null key.
- Synchronized and Unsynchronized Versions: Java provides both synchronized and unsynchronized versions of HashMap.
- 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:
javaimport 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:
- We import the
HashMap
class from thejava.util
package. - We create a
HashMap
instance calledstudentMap
with Integer keys and String values. - We add elements to the HashMap using the
put()
method, where the first argument is the key and the second argument is the value. - We retrieve a value from the HashMap using the
get()
method, providing the key as an argument. - We remove an element from the HashMap using the
remove()
method, passing the key to be removed. - We check if a key exists in the HashMap using the
containsKey()
method. - We iterate through the HashMap using a for-each loop and
keySet()
. - 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.
0 Comments