Package io.bytestreams.codec.core.util
Class BiMap<K,V>
java.lang.Object
io.bytestreams.codec.core.util.BiMap<K,V>
- Type Parameters:
K- the key typeV- the value type
- All Implemented Interfaces:
Converter<K,V>
An immutable bidirectional map that maintains a one-to-one mapping between keys and values.
Both keys and values must be unique. Lookups can be performed in either direction: by key
using to(Object) or by value using from(Object).
Example usage:
BiMap<Integer, String> biMap = BiMap.of(
Map.entry(1, "one"),
Map.entry(2, "two"),
Map.entry(3, "three")
);
biMap.to(1); // returns "one"
biMap.from("two"); // returns 2
-
Method Details
-
of
Creates a newBiMapfrom the given entries.- Type Parameters:
K- the key typeV- the value type- Parameters:
entries- the key-value entries- Returns:
- a new
BiMap - Throws:
IllegalArgumentException- if entries is empty, or contains duplicate keys or valuesNullPointerException- if any entry, key, or value is null
-
to
Returns the value mapped to the given key.- Specified by:
toin interfaceConverter<K,V> - Parameters:
key- the key to look up- Returns:
- the value mapped to the key
- Throws:
IllegalArgumentException- if the key is not present
-
from
Returns the key mapped to the given value.- Specified by:
fromin interfaceConverter<K,V> - Parameters:
value- the value to look up- Returns:
- the key mapped to the value
- Throws:
IllegalArgumentException- if the value is not present
-