Guava BiMap Example

0

You want to keep track of both keys and values. You also want the values to be unique. If you want to go the traditional way using HashMap, you will have to maintain two separate maps, one for key-value and the other for value->key.

  1. Map<Key, Value>
  2. Map<Value, Key>

The main issue here would be to maintain the maps. Use cases would be:

  1. When a key/value pair is added to the Map<Key, Value>, we also need to add an additional inverse pair that maps value to key. Thus both the maps need to be kept in sync
  2. If a key is already present, its value will be replaced with the new one. If one is trying to add value as key to the inverse map, this should be treated as an exception.

Guava BiMap is a “bidirectional map” with a goal to preserve the uniqueness of its values as well as that of its keys. This constraint enables BiMap to support an “inverse view”, which is another BiMap containing the same entries.

In the below example, we create a BiMap using HashBiMap.

BiMap<String, String> biMap = HashBiMap.create();

HashBiMap internally uses two hash tables to maintain key to value and value to key mappings.
In order to obtain the inverse view, we will call the inverse() method on BiMap.

BiMap.inverse()

If a value is already present and we try to add it again then a code>IllegalArgumentException will be thrown.

...
biMap.put(COLOR, YELLOW);
...
try {
    biMap.put(CHAIR, YELLOW);
} catch (IllegalArgumentException e) {
    System.out.println(e);
}

If we still want to go ahead and add the value then we need to use forcePut(key, value).

biMap.forcePut(CHAIR,  YELLOW);

BiMapExample:

package com.javarticles.guava;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;


public class BiMapExample {
    private static final String COLOR = "Color";
    private static final String RED = "Red";
    private static final String YELLOW = "Yellow";
    
    private static final String FURNITURE = "Furniture";
    private static final String CHAIR = "Chair";
    
    public static void main(String[] args) {
        BiMap<String, String> biMap = HashBiMap.create();
        biMap.put(COLOR, RED);
        biMap.put(COLOR, YELLOW);
        System.out.println("Total size: " + biMap.size());
     
        biMap.put(FURNITURE, CHAIR);
        biMap.put(CHAIR, RED);
        
        System.out.println("Total size: " + biMap.size());        
        System.out.println("BiMap Items: " + biMap);        
        System.out.println("Inverse Items: " + biMap.inverse());
        
        
        try {
            biMap.put(CHAIR, YELLOW);
        } catch (IllegalArgumentException e) {
            System.out.println(e);
        }
        
        System.out.println("Force put chair->yellow");
        biMap.forcePut(CHAIR,  YELLOW);
        
        System.out.println("Total size: " + biMap.size());        
        System.out.println("BiMap Items: " + biMap);
    }   
}

Output:

Total size: 1
Total size: 3
BiMap Items: {Furniture=Chair, Color=Yellow, Chair=Red}
Inverse Items: {Chair=Furniture, Yellow=Color, Red=Chair}
java.lang.IllegalArgumentException: value already present: Yellow
Force put chair->yellow
Total size: 2
BiMap Items: {Furniture=Chair, Chair=Yellow}

Download the source code

This was an example about Guava BiMap.

You can download the source code: guavaBitMapExample.zip
Share.

Comments are closed.