Package org.ethelred.util.collect
Class BiMap<A,B>
- java.lang.Object
-
- org.ethelred.util.collect.BiMap<A,B>
-
- Type Parameters:
A- Type of one of the keys/valuesB- Type of the other keys/values
public class BiMap<A,B> extends java.lang.ObjectA 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 classBiMap.Entry<A,B>
-
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)Get the B value corresponding to the A key.java.util.Optional<A>getByB(B key)Get the A value corresponding to the B key.java.util.Set<A>keysA()java.util.Set<B>keysB()java.util.Map<A,B>mapByA()java.util.Map<B,A>mapByB()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.intsize()static <A,B>
java.util.stream.Collector<BiMap.Entry<A,B>,?,BiMap<A,B>>toBiMap()
-
-
-
Method Detail
-
size
public int size()
-
getByA
public java.util.Optional<B> getByA(A key)
Get the B value corresponding to the A key.- Parameters:
key- A key- Returns:
- Optional containing value, empty if not found
-
getByB
public java.util.Optional<A> getByB(B key)
Get the A value corresponding to the B key.- Parameters:
key- B key- Returns:
- Optional containing value, empty if not found
-
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/valueB- the type of the second key/value- Parameters:
a- one of the keys/valuesb- 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
-
keysA
public java.util.Set<A> keysA()
- Returns:
- An unmodifiable view of the set of A keys
-
keysB
public java.util.Set<B> keysB()
- Returns:
- An unmodifiable view of the set of B keys
-
toBiMap
public static <A,B> java.util.stream.Collector<BiMap.Entry<A,B>,?,BiMap<A,B>> toBiMap()
- Type Parameters:
A- Type of one of the keys/valuesB- Type of the other keys/values- Returns:
- Collect a stream of Entry into a BiMap
-
-