Guava BiMap Example (Bidirectional Map)
Guava BiMap
A special type of collection that ensures uniqueness of both keys and values. A BiMap or Bidirectional Map ensures that all values are unique — this ensures that all values are a set. I would best explain BiMap as having a mapping of keys to values and another mapping of values to keys. Such that, each set must contain unique elements.
Methods available to BiMap
@GwtCompatible
public interface BiMap<K,V>
extends Map<K,V>
# | Method and Description |
---|---|
1 | V forcePut(K key, V value) An alternate form of put that silently removes any existing entry with the value value before proceeding with the put(K, V) operation. |
2 | BiMap<V,K> inverse() Returns the inverse view of this BiMap, which maps each of this BiMap’s values to its associated key. |
3 | V put(K key, V value) Associates the specified value with the specified key in this map (optional operation). |
4 | void putAll(Map<? extends K,? extends V> map) Copies all of the mappings from the specified map to this map (optional operation). |
5 | Set<V> values() Returns a Collection view of the values contained in this map. |
BiMap Implementations
There are four implementation classes available for BiMap:
- EnumBiMap
- EnumHashBiMap
- HashBiMap
- ImmutableBiMap
Java Example using Guava BiMap
In this BiMap Example we are using HashBiMap to create a BiMap of States and their Capitals called StateCapitals. We then populate our stateCapitals BiMap with all of our states and their respective capitals. The two methods I have created enable us to pass either the key and return the respective value as is the case in the printCapitalOfState method. Additionally, we can pass the value of a mapping and return the key as is the case in the printStateOfCapital method using the inverse() method.
package com.avaldes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; public class GuavaBiMapExample { private static BiMap<String, String> stateCapitals = HashBiMap.create(); public static void main(String[] args) { Logger logger = LoggerFactory.getLogger(GuavaBiMapExample.class); // Let's create a BiMap containing states and their respective capitals // eg. New Jersey -> Trenton logger.info("Adding some states and their capitals..."); stateCapitals.put("Alabama", "Montgomery"); stateCapitals.put("Alaska", "Juneau"); stateCapitals.put("Arizona", "Phoenix"); stateCapitals.put("Arkansas", "Little Rock"); stateCapitals.put("California", "Sacramento"); stateCapitals.put("Colorado", "Denver"); stateCapitals.put("Connecticut", "Hartford"); stateCapitals.put("Delaware", "Dover"); stateCapitals.put("Florida", "Tallahassee"); stateCapitals.put("Georgia", "Atlanta"); stateCapitals.put("Hawaii", "Honolulu"); stateCapitals.put("Idaho", "Boise"); stateCapitals.put("Illinois", "Springfield"); stateCapitals.put("Indiana", "Indianapolis"); stateCapitals.put("Iowa", "Des Moines"); stateCapitals.put("Kansas", "Topeka"); stateCapitals.put("Kentucky", "Frankfort"); stateCapitals.put("New Jersey", "Trenton"); stateCapitals.put("New York", "Albany"); System.out.println("\n--[States to Capitals]----"); printCapitalOfState("Arizona"); printCapitalOfState("Hawaii"); printCapitalOfState("New Jersey"); printCapitalOfState("Florida"); printCapitalOfState("Connecticut"); System.out.println("\n--[Capitals to States]----"); printStateOfCapital("Springfield"); printStateOfCapital("Topeka"); printStateOfCapital("Little Rock"); printStateOfCapital("Albany"); printStateOfCapital("Montgomery"); } public static void printCapitalOfState(String state) { if (stateCapitals.containsKey(state)) { System.out.println("State " + state +", capital is: " + stateCapitals.get(state)); } else { System.out.println("State not found..."); } } public static void printStateOfCapital(String capital) { if (stateCapitals.containsValue(capital)) { System.out.println("Capital " + capital +", state is: " + stateCapitals.inverse().get(capital)); } else { System.out.println("Capital not found..."); } } }
That’s It!
I hope you enjoyed this tutorial. Please continue to share the love and like us so that we can continue bringing you quality tutorials. Happy Guava Coding!!!
Related Posts
- Guava Multimap - ArrayListMultimap Implementation Example
In this tutorial we will cover implementing an ArrayListMultimap (which allows duplicate key/value pairs). - Guava Multimap - HashMultimap Implementation Example
In this tutorial we will cover implementing an HashMultimap which does not allow duplicate key/value pairs, using HashMultimap. - Guava Multimap - TreeMultimap Implementation Example
In this tutorial we will cover a implementing an TreeMultimap which performs Sorting according to natural order and does not allow duplicate key/value pairs. - Guava Multiset Example
In this tutorial we will cover a basic introduction the to Guava's Multiset collection using easy to follow example to show you how best to implement this collection in your own code. - Guava BiMap Example (Bi-Directional Map)
In this tutorial we will cover a basic introduction the to Guava's Bidirectional Map (BiMap) using easy to follow example to show you how best to implement this collection in your own code. - Guava Table Example
In this tutorial we will cover a basic introduction the to Guava's Table using easy to follow example to show you how best to implement this collection in your own code. - Guava ClassToInstanceMap Example
In this tutorial we show you how to use Guava's ClassToInstanceMap collection to store class type as the key and the instance of the collection as a value.
Leave a Reply