Map
About
The Map
interface in Java is part of the java.util
package and represents a collection of key-value pairs. It provides an abstract data structure where each key is mapped to a specific value, enabling efficient data retrieval based on unique keys. Unlike a Collection
, a Map
does not allow duplicate keys, but it permits duplicate values.
Introduced in Java 1.2, the
Map
interface forms a core part of the Java Collections Framework.It is widely used for tasks such as data indexing, lookups, caching, and configuration management.
Maps are unordered by default unless we use implementations like
LinkedHashMap
orTreeMap
.
Features
Key-Value Pairs: Each entry in a
Map
consists of a unique key and an associated value. The uniqueness of keys ensures efficient access.No Duplicate Keys: While values can be duplicated, keys must be unique. Adding a duplicate key will replace the previous mapping.
Various Implementations:
HashMap
: Fast, unordered, allows onenull
key and multiplenull
values.TreeMap
: Ordered based on keys' natural ordering or a provided comparator.LinkedHashMap
: Maintains insertion or access order.
Versatility: A
Map
can store objects of different types using generic parameters, e.g.,Map<String, Integer>
.Thread Safety:
Basic
Map
implementations likeHashMap
are not thread-safe.Concurrent implementations such as
ConcurrentHashMap
ensure thread safety.
Functional Programming Support: From Java 8 onwards,
Map
includes several functional-style methods likeforEach
,compute
,merge
, andreplaceAll
.
Key Methods
Method
Description
put(K key, V value)
Associates the specified value with the specified key in the map.
get(Object key)
Retrieves the value associated with the given key, or null
if the key is not present.
remove(Object key)
Removes the mapping for the specified key, if it exists.
containsKey(Object key)
Returns true
if the map contains the specified key.
containsValue(Object value)
Returns true
if the map contains one or more keys mapped to the specified value.
keySet()
Returns a Set
view of all the keys in the map.
values()
Returns a Collection
view of all the values in the map.
entrySet()
Returns a Set
view of all the key-value mappings (Map.Entry<K, V>
objects).
size()
Returns the number of key-value pairs in the map.
isEmpty()
Checks if the map is empty.
clear()
Removes all key-value mappings from the map.
putIfAbsent(K key, V value)
Adds the value only if the key is not already associated with another value.
compute(K key, BiFunction)
Computes a value for the specified key using the given function.
merge(K key, V value, BiFunction)
Merges the existing value with the provided one using the given function.
Map Implementations
1. HashMap
HashMap
Features:
Backed by a hash table.
No ordering of keys or values.
Allows
null
key and multiplenull
values.
Performance:
O(1) for
put
andget
operations in ideal conditions.
Use Case: General-purpose map for fast lookups.
2. LinkedHashMap
LinkedHashMap
Features:
Maintains insertion order or access order (configurable).
Performance: Slightly slower than
HashMap
.Use Case: LRU (Least Recently Used) caches or when iteration order matters.
3. TreeMap
TreeMap
Features:
Implements
NavigableMap
.Sorted based on natural ordering or a custom comparator.
Performance: O(log n) for insertion, deletion, and lookup.
Use Case: Sorted map for ranges and queries.
4. Hashtable
Hashtable
Features:
Legacy class.
Thread-safe but slower than modern alternatives.
Does not allow
null
keys or values.
Use Case: Avoid unless compatibility with older code is required.
5. ConcurrentHashMap
ConcurrentHashMap
Features:
Thread-safe.
Allows concurrent access without locking the entire map.
Use Case: Multi-threaded environments.
6. EnumMap
EnumMap
Features:
Designed specifically for
enum
keys.Backed by an array, making it fast and memory-efficient.
Use Case: Maps with fixed, predefined keys.
Custom Implementations of Map
Why Create a Custom Map?
Custom Map
implementations are useful when the standard implementations do not meet specific requirements, such as:
Custom hashing or equality logic.
Special behavior for specific keys or values.
Optimized performance for a particular use case.
Steps to Implement a Custom Map
Implement the
Map
Interface:The
Map
interface has 16 methods to implement.We can extend
AbstractMap
to simplify the process as it provides default implementations for some methods.
Key Points to Consider:
Storage: Decide how the key-value pairs will be stored (e.g., array, linked list, tree).
Hashing and Equality: Define custom logic for hashing or equality if necessary.
Concurrency: Ensure thread safety if required.
Example: Custom Fixed-Size Map
Use Case of Custom Map
A fixed-size map where additional inserts beyond a certain limit throw an exception.
Maps with encryption for keys or values.
Last updated
Was this helpful?