Package com.landawn.abacus.util
Class BiMap<K,V>
java.lang.Object
com.landawn.abacus.util.BiMap<K,V>
- Type Parameters:
K
- the key typeV
- the value type
- All Implemented Interfaces:
Map<K,
V>
A BiMap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys.
This constraint enables BiMaps to support an "inverse view", which is another BiMap containing the same entries as this BiMap but with reversed keys and values.
- Since:
- 0.8
- Author:
- Haiyang Li
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clear.boolean
containsKey
(Object key) boolean
containsValue
(Object value) copy()
static <K,
V> BiMap<K, V> entrySet()
Returns an immutable Set of Immutable entry.boolean
An alternate form ofput
that silently removes any existing entry with the valuevalue
before proceeding with theput(K, V)
operation.getByValue
(Object value) Gets the by value.int
hashCode()
inversed()
Returns the inverse view of this BiMap, which maps each of this bimap's values to its associated key.boolean
isEmpty()
Checks if is empty.keySet()
Returns an immutable key set.static <K,
V> BiMap<K, V> of
(K k1, V v1) static <K,
V> BiMap<K, V> of
(K k1, V v1, K k2, V v2) static <K,
V> BiMap<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3) static <K,
V> BiMap<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) static <K,
V> BiMap<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) static <K,
V> BiMap<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) static <K,
V> BiMap<K, V> of
(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) void
Warning: the results of calling this method may vary depending on the iteration order ofmap
.removeByValue
(Object value) Removes the by value.int
size()
toString()
values()
Returns an immutable value set.Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
BiMap
public BiMap() -
BiMap
public BiMap(int initialCapacity) - Parameters:
initialCapacity
-
-
BiMap
public BiMap(int initialCapacity, float loadFactor) - Parameters:
initialCapacity
-loadFactor
-
-
BiMap
- Parameters:
keyMapType
-valueMapType
-
-
BiMap
public BiMap(Supplier<? extends Map<K, V>> keyMapSupplier, Supplier<? extends Map<V, K>> valueMapSupplier) - Parameters:
keyMapSupplier
-valueMapSupplier
-
-
-
Method Details
-
of
- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
-v1
-- Returns:
-
of
- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
-v1
-k2
-v2
-- Returns:
-
of
- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
-v1
-k2
-v2
-k3
-v3
-- Returns:
-
of
- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
-v1
-k2
-v2
-k3
-v3
-k4
-v4
-- Returns:
-
of
- Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
-v1
-k2
-v2
-k3
-v3
-k4
-v4
-k5
-v5
-- Returns:
-
of
public static <K,V> BiMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) - Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
-v1
-k2
-v2
-k3
-v3
-k4
-v4
-k5
-v5
-k6
-v6
-- Returns:
-
of
public static <K,V> BiMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) - Type Parameters:
K
- the key typeV
- the value type- Parameters:
k1
-v1
-k2
-v2
-k3
-v3
-k4
-v4
-k5
-v5
-k6
-v6
-k7
-v7
-- Returns:
-
copyOf
- Type Parameters:
K
- the key typeV
- the value type- Parameters:
map
-- Returns:
-
get
-
getByValue
Gets the by value.- Parameters:
value
-- Returns:
-
put
- Specified by:
put
in interfaceMap<K,
V> - Parameters:
key
-value
-- Returns:
- Throws:
IllegalArgumentException
- if the given value is already bound to a different key in this bimap. The bimap will remain unmodified in this event. To avoid this exception, callforcePut(K, V)
instead.
-
forcePut
An alternate form ofput
that silently removes any existing entry with the valuevalue
before proceeding with theput(K, V)
operation. If the bimap previously contained the provided key-value mapping, this method has no effect.Note that a successful call to this method could cause the size of the bimap to increase by one, stay the same, or even decrease by one.
Warning: If an existing entry with this value is removed, the key for that entry is discarded and not returned.
- Parameters:
key
- the key with which the specified value is to be associatedvalue
- the value to be associated with the specified key- Returns:
- be
null
, ornull
if there was no previous entry
-
putAll
Warning: the results of calling this method may vary depending on the iteration order of
map
.- Specified by:
putAll
in interfaceMap<K,
V> - Parameters:
m
-- Throws:
IllegalArgumentException
- if an attempt toput
any entry fails. Note that some map entries may have been added to the bimap before the exception was thrown.
-
remove
-
removeByValue
Removes the by value.- Parameters:
value
-- Returns:
-
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V> - Parameters:
key
-- Returns:
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,
V> - Parameters:
value
-- Returns:
-
keySet
Returns an immutable key set. -
values
Returns an immutable value set. -
entrySet
Returns an immutable Set of Immutable entry. -
inversed
Returns the inverse view of this BiMap, which maps each of this bimap's values to its associated key. The two BiMaps are backed by the same data; any changes to one will appear in the other.- Returns:
-
copy
- Returns:
-
clear
public void clear()Clear. -
isEmpty
public boolean isEmpty()Checks if is empty. -
size
public int size() -
hashCode
public int hashCode() -
equals
-
toString
-