Package com.pnfsoftware.jeb.util.collect
Class BiMap<K,V>
java.lang.Object
com.pnfsoftware.jeb.util.collect.BiMap<K,V>
- Type Parameters:
K- key type; must have correct equals/hashCodeV- value type; must have correct equals/hashCode
A bidirectional map of key-values. This class is
serializable. The null key and
null value are forbidden. The key-value mapping is one-to-one: a key can only map to one value,
and one value can only map to one key. (One consequence is that putting a key-value pair may either increment, keep the same, or even decrement the map size.
Thread-safety: none.
Note: this class does not implement the Map interface, but attempts to follow its
guidelines and contracts the best it can. One notable exception is that this class does not
override hashCode/equals.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionasMap()Get a read-only view of the forward map.Get a read-only view of the reverse map.voidclear()Remove all mappings.booleancontainsKey(K key) Determine whether a key is present.booleancontainsValue(V value) Determine whether a value is present.Retrieve a value by key.getKeyForValue(V value) Retrieve a key by value.booleanisEmpty()Determine whether this map is empty.keySet()Get a view of the keys.Add a one-to-one key-value mapping.voidAdd all mappings from a map.Remove a mapping by key.removeValue(V value) Remove a mapping by value.intsize()Get the number of mappings.toString()values()Get a view of the values.
-
Constructor Details
-
BiMap
public BiMap()Create a new bi-map. -
BiMap
Create a copy of an existing bi-map.- Parameters:
o- source bi-map
-
BiMap
Create a new bi-map.- Parameters:
keyOrder- key ordering
-
-
Method Details
-
clear
public void clear()Remove all mappings. -
isEmpty
public boolean isEmpty()Determine whether this map is empty.- Returns:
- true if no mapping exists
-
size
public int size()Get the number of mappings.- Returns:
- number of mappings
-
put
Add a one-to-one key-value mapping.- Parameters:
key- non-null keyvalue- non-null value- Returns:
- value previously associated with the key, or null
-
putAll
Add all mappings from a map.- Parameters:
m- source map
-
containsKey
Determine whether a key is present.- Parameters:
key- key- Returns:
- true if the key is present
-
containsValue
Determine whether a value is present.- Parameters:
value- value- Returns:
- true if the value is present
-
get
Retrieve a value by key.- Parameters:
key- key- Returns:
- value associated with the key, or null
-
getKeyForValue
Retrieve a key by value.- Parameters:
value- value- Returns:
- key associated with the value, or null
-
remove
Remove a mapping by key.- Parameters:
key- key- Returns:
- removed value, or null
-
removeValue
Remove a mapping by value.- Parameters:
value- value- Returns:
- removed key, or null
-
keySet
Get a view of the keys.- Returns:
- key set view
-
values
Get a view of the values.- Returns:
- value set view
-
asMap
Get a read-only view of the forward map.- Returns:
- read-only forward map
-
asReverseMap
Get a read-only view of the reverse map.- Returns:
- read-only reverse map
-
toString
-