Class BiMap<K,V>

java.lang.Object
io.bytestreams.codec.core.util.BiMap<K,V>
Type Parameters:
K - the key type
V - the value type
All Implemented Interfaces:
Converter<K,V>

public final class BiMap<K,V> extends Object implements 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

      @SafeVarargs public static <K, V> BiMap<K,V> of(Map.Entry<K,V>... entries)
      Creates a new BiMap from the given entries.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      entries - the key-value entries
      Returns:
      a new BiMap
      Throws:
      IllegalArgumentException - if entries is empty, or contains duplicate keys or values
      NullPointerException - if any entry, key, or value is null
    • to

      public V to(K key)
      Returns the value mapped to the given key.
      Specified by:
      to in interface Converter<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

      public K from(V value)
      Returns the key mapped to the given value.
      Specified by:
      from in interface Converter<K,V>
      Parameters:
      value - the value to look up
      Returns:
      the key mapped to the value
      Throws:
      IllegalArgumentException - if the value is not present