- Collection Framework in JavaCollections Class in JavaList Interface in Java with ExamplesArrayList in JavaVector Class in JavaLinkedList in JavaQueue Interface In JavaPriorityQueue in JavaSet in JavaHashSet in JavaLinkedHashSet in JavaMap in the JavaHashMap in JavaHashtable in JavaLinkedHashMap in Java
HashMap in Java
HashMap is a class in the Java Collection framework that implements the Map interface. It stores the data in key-value pairs and allows us to access the values using their associated keys. HashMap is an unordered collection and it does not guarantee the order of the elements.
HashMap uses an array of buckets to store the key-value pairs. When a key-value pair is inserted, the key is hashed and the resulting hash code is used to calculate the index of the bucket where the pair will be stored. If there are multiple key-value pairs that have the same hash code, they are stored in the same bucket as a linked list of entries. The linked list of entries is searched sequentially to find the key-value pair with the matching key.
Here's an example of how to use a HashMap in Java
import java.util.HashMap;In this example, we first create a new HashMap with key type
public class HashMapExample {
public static void main(String[] args) {
// Create a new HashMap
HashMap<String, Integer> map = new HashMap<>();
// Add key-value pairs to the map
map.put("John", 25);
map.put("Jane", 30);
map.put("Alice", 35);
// Get the value associated with a key
int age = map.get("John");
System.out.println("John's age is " + age);
// Check if a key exists in the map
if (map.containsKey("Jane")) {
System.out.println("Jane's age is " + map.get("Jane"));
}
// Remove a key-value pair from the map
map.remove("Alice");
// Iterate over the key-value pairs in the map
for (String key : map.keySet()) {
int value = map.get(key);
System.out.println(key + "'s age is " + value);
}
}
}
String
and value type Integer
. We then add three key-value pairs to the map using the put
method. We retrieve the value associated with the key "John" using the get
method and print it to the console. We check if the key "Jane" exists in the map using the containsKey
method and print her age to the console. We remove the key-value pair with the key "Alice" using the remove
method. Finally, we iterate over the key-value pairs in the map using a for-each loop and print their values to the console.HashMap has several important features
Duplicate keys are not allowed. If you try to add a key-value pair with a key that already exists in the map, the old value will be overwritten with the new value.
Null keys and null values are allowed. You can add a key-value pair with a null key or a null value to the map.
HashMap is not thread-safe. If you need to use a HashMap in a multi-threaded environment, you should use the
ConcurrentHashMap
class instead.The order of the key-value pairs in the map is not guaranteed. If you need to maintain the order of the elements, you can use the
LinkedHashMap
class instead.
Overall, HashMap is a useful data structure in Java for storing and accessing key-value pairs. It has good performance for most operations and is easy to use.