Class BiMap<A,​B>

  • Type Parameters:
    A - Type of one of the keys/values
    B - Type of the other keys/values

    public class BiMap<A,​B>
    extends java.lang.Object
    A bidirectional mapping. Does not implement Map because I don't find that useful now. May not contain nulls or duplicate keys/values. Immutable.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  BiMap.Entry<A,​B>  
    • Constructor Summary

      Constructors 
      Constructor Description
      BiMap​(java.util.Map<A,​B> map)
      Construct a BiMap from the entries in map.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <A,​B>
      BiMap.Entry<A,​B>
      entry​(A a, B b)
      Construct an entry for building a BiMap.
      java.util.Optional<B> getByA​(A key)  
      java.util.Optional<A> getByB​(B key)  
      static <A,​B>
      BiMap<A,​B>
      ofEntries​(java.lang.Iterable<BiMap.Entry<A,​B>> entries)
      Construct a BiMap from given entries.
      static <A,​B>
      BiMap<A,​B>
      ofEntries​(java.util.Collection<BiMap.Entry<A,​B>> entries)
      Construct a BiMap from given entries.
      static <A,​B>
      BiMap<A,​B>
      ofEntries​(BiMap.Entry<A,​B>... entries)
      Construct a BiMap from given entries.
      int size()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BiMap

        public BiMap​(java.util.Map<A,​B> map)
        Construct a BiMap from the entries in map.
        Parameters:
        map - entries to use
        Throws:
        java.lang.NullPointerException - when any key or value in map is null
        java.lang.IllegalArgumentException - when there are duplicate keys or values
    • Method Detail

      • size

        public int size()
      • getByA

        public java.util.Optional<B> getByA​(A key)
      • getByB

        public java.util.Optional<A> getByB​(B key)
      • entry

        public static <A,​B> BiMap.Entry<A,​B> entry​(A a,
                                                               B b)
        Construct an entry for building a BiMap.
        Type Parameters:
        A - the type of the first key/value
        B - the type of the second key/value
        Parameters:
        a - one of the keys/values
        b - the other key/value
        Returns:
        an entry
      • ofEntries

        public static <A,​B> BiMap<A,​B> ofEntries​(java.lang.Iterable<BiMap.Entry<A,​B>> entries)
        Construct a BiMap from given entries.
        Parameters:
        entries - entries to use
        Throws:
        java.lang.IllegalArgumentException - when there are duplicate keys or values
      • ofEntries

        public static <A,​B> BiMap<A,​B> ofEntries​(java.util.Collection<BiMap.Entry<A,​B>> entries)
        Construct a BiMap from given entries.
        Parameters:
        entries - entries to use
        Throws:
        java.lang.IllegalArgumentException - when there are duplicate keys or values
      • ofEntries

        @SafeVarargs
        public static <A,​B> BiMap<A,​B> ofEntries​(BiMap.Entry<A,​B>... entries)
        Construct a BiMap from given entries.
        Parameters:
        entries - entries to use
        Throws:
        java.lang.IllegalArgumentException - when there are duplicate keys or values