The Map interface in Java is part of the Java Collections Framework, but it is not a subtype of the Collection interface. Unlike collections, which are used to store single elements, a Map is used to store key-value pairs, where each key is unique, and each key maps to exactly one value.
Map
consists of a key and a corresponding value. Keys must be unique, but values can be duplicated.Map
may or may not maintain the order of the keys.HashMap
but synchronized.==
) instead of equals()
.Method | Description |
int size() | Returns the number of key-value mappings in the map.
|
boolean isEmpty() | Returns
|
V get(Object key) | Returns the value to which the specified key is mapped, or
|
V put(K key, V value) | Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
NOTE: Return Type: V (the previous value associated with the key, or |
V remove(Object key) | Removes the mapping for a key from this map if it is present.
NOTE: Return Type: V (the previous value associated with the key, or |
boolean remove(Object key, Object value) | Removes the entry for the specified key only if it is currently mapped to the specified value.
|
boolean containsKey(Object key) | Returns true if this map contains a mapping for the specified key.
|
boolean containsValue(Object value) | Returns true if this map maps one or more keys to the specified value.
|
void putAll(Map<K, V> m) | Copies all of the mappings from the specified map to this map.
|
void clear() | Removes all of the mappings from this map.
|
Set<K> keySet() | Returns a Set view of the keys contained in this map.
|
Collection<V> values() | Returns a Collection view of the values contained in this map.
|
Set<Map.Entry<K, V>> entrySet() | Returns a Set view of the mappings contained in this map. Each element in the returned set is a Map.Entry Object.
|
boolean equals(Object o) | Compares the specified object with the map for equality.
|
V getOrDefault(Object key, V defaultValue) | Returns the value to which the specified key is mapped, or defaultValue if the map contains no mapping for the key.
|
V putIfAbsent(K key, V value) | If the specified key is not already associated with a value (or is mapped to
|
boolean replace(K key, V oldValue, V newValue) | Replaces the entry for the specified key only if currently mapped to the specified value.
|
V replace(K key, V value) | Replaces the entry for the specified key only if it is currently mapped to some value.
|
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Collection;
public class MapExample {
public static void main(String[] args) {
// Create a HashMap
Map<String, Integer> map = new HashMap<>();
// Add key-value pairs to the map
map.put("Apple", 10);
map.put("Banana", 15);
map.put("Orange", 20);
// Get the size of the map
System.out.println("Map size: " + map.size());
// Check if a key exists
System.out.println("Contains 'Apple': " + map.containsKey("Apple"));
// Get a value from the map
System.out.println("Value for 'Banana': " + map.get("Banana"));
// Remove a key-value pair
map.remove("Orange");
// Iterate over the keys
Set<String> keys = map.keySet();
System.out.println("Keys: " + keys);
// Iterate over the values
Collection<Integer> values = map.values();
System.out.println("Values: " + values);
// Iterate over the entries
Set<Map.Entry<String, Integer>> entries = map.entrySet();
System.out.println("Entries:");
for (Map.Entry<String, Integer> entry : entries) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
// Clear the map
map.clear();
System.out.println("Is map empty after clear: " + map.isEmpty());
}
}
Contains 'Apple': true
Value for 'Banana': 15
Keys: [Apple, Banana]
Values: [10, 15]
Entries:
Apple = 10
Banana = 15
Is map empty after clear: true