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
1V 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.
2BiMap<V,K> inverse()
Returns the inverse view of this BiMap, which maps each of this BiMap’s values to its associated key.
3V put(K key, V value)
Associates the specified value with the specified key in this map (optional operation).
4void putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map (optional operation).
5Set<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!!!

GuavaBiMap

Related Posts

Please Share Us on Social Media

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *