Class BidiMap<K,V>
- Type Parameters:
K- The key type.V- The value type.
- All Implemented Interfaces:
Map<K,V>
This implementation provides efficient bidirectional lookups by maintaining two internal maps: a forward map for key-to-value lookups and a reverse map for value-to-key lookups.
Features:
- Implements the standard
Mapinterface for forward key→value lookups - Provides
getKey(Object)method for reverse value→key lookups - Maintains insertion order using
LinkedHashMapinternally - Automatically filters out null keys and values
- Supports both mutable and unmodifiable instances via the builder
- Thread-safety: Not thread-safe by default; external synchronization required if accessed by multiple threads
Usage:
Null Handling:
This map automatically filters out entries with null keys or values during construction.
Attempting to add null keys or values via put(Object, Object) or putAll(Map)
after construction will result in them being stored in the forward map but not the reverse map.
Unmodifiable Instances:
See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all key-value mappings from this map.booleancontainsKey(Object key) Returnstrue if this map contains a mapping for the specified key.booleancontainsValue(Object value) Returnstrue if this map maps one or more keys to the specified value.static <K,V> BidiMap.Builder<K, V> create()Creates a new builder for this class.entrySet()Returns aSetview of the mappings contained in this map.booleanCompares the specified object with this map for equality.Returns the value to which the specified key is mapped, ornull if this map contains no mapping for the key.Returns the key that is currently mapped to the specified value.inthashCode()Returns the hash code value for this map.booleanisEmpty()Returnstrue if this map contains no key-value mappings.keySet()Returns aSetview of the keys contained in this map.Associates the specified value with the specified key in this map.voidCopies all mappings from the specified map to this map.Removes the mapping for a key from this map if it is present.intsize()Returns the number of key-value mappings in this map.toString()Returns a string representation of this map.values()Returns aCollectionview of the values contained in this map.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
BidiMap
Constructor.Constructs a bidirectional map from the provided builder, automatically filtering out any entries with null keys or values.
- Parameters:
builder- The builder containing the initial entries.
-
-
Method Details
-
create
Creates a new builder for this class.- Type Parameters:
K- The key type.V- The value type.- Returns:
- A new
BidiMap.Builderinstance.
-
clear
Removes all key-value mappings from this map.Clears both the forward and reverse maps.
- Specified by:
clearin interfaceMap<K,V> - Throws:
UnsupportedOperationException- if the map is unmodifiable.
-
containsKey
Returnstrue if this map contains a mapping for the specified key.- Specified by:
containsKeyin interfaceMap<K,V> - Parameters:
key- The key to check for.- Returns:
true if this map contains a mapping for the specified key.
-
containsValue
Returnstrue if this map maps one or more keys to the specified value.This implementation uses the reverse map for efficient lookup.
- Specified by:
containsValuein interfaceMap<K,V> - Parameters:
value- The value to check for.- Returns:
true if this map maps one or more keys to the specified value.
-
entrySet
Returns aSetview of the mappings contained in this map. -
get
Returns the value to which the specified key is mapped, ornull if this map contains no mapping for the key. -
getKey
Returns the key that is currently mapped to the specified value.This is the reverse lookup operation that makes this a bidirectional map.
Example:
BidiMap<String,Integer>
map = BidiMap.create ().add("two" , 2).build(); Stringkey =map .getKey(2);// Returns "two" - Parameters:
value- The value whose associated key is to be returned.- Returns:
- The key to which the specified value is mapped, or
null if this map contains no mapping for the value.
-
isEmpty
Returnstrue if this map contains no key-value mappings. -
keySet
Returns aSetview of the keys contained in this map. -
put
Associates the specified value with the specified key in this map.This operation updates both the forward map (key→value) and the reverse map (value→key).
- Specified by:
putin interfaceMap<K,V> - Parameters:
key- The key with which the specified value is to be associated.value- The value to be associated with the specified key.- Returns:
- The previous value associated with the key, or
null if there was no mapping for the key. - Throws:
UnsupportedOperationException- if the map is unmodifiable.IllegalArgumentException- if the value already exists mapped to a different key.
-
putAll
Copies all mappings from the specified map to this map.This operation updates both the forward and reverse maps.
- Specified by:
putAllin interfaceMap<K,V> - Parameters:
m- Mappings to be stored in this map.- Throws:
UnsupportedOperationException- if the map is unmodifiable.IllegalArgumentException- if any value in the map already exists mapped to a different key.
-
remove
Removes the mapping for a key from this map if it is present.This operation removes the entry from both the forward and reverse maps.
- Specified by:
removein interfaceMap<K,V> - Parameters:
key- The key whose mapping is to be removed from the map.- Returns:
- The previous value associated with the key, or
null if there was no mapping for the key. - Throws:
UnsupportedOperationException- if the map is unmodifiable.
-
size
Returns the number of key-value mappings in this map. -
values
Returns aCollectionview of the values contained in this map. -
toString
Returns a string representation of this map.The format follows the standard Java map convention:
"{key1=value1, key2=value2, ...}" -
equals
Compares the specified object with this map for equality.Returns
true if the given object is also a map and the two maps represent the same mappings. More formally, two mapsm1 andm2 represent the same mappings ifm1.entrySet().equals(m2.entrySet()) .This implementation compares the entry sets of the two maps.
-
hashCode
Returns the hash code value for this map.The hash code of a map is defined to be the sum of the hash codes of each entry in the map's
entrySet() view. This ensures thatm1.equals(m2) implies thatm1.hashCode()==m2.hashCode() for any two mapsm1 andm2 , as required by the general contract ofObject.hashCode().This implementation computes the hash code from the entry set.
-