Guava - Bimap Interface


Advertisements

A BiMap is a special kind of map which maintains an inverse view of the map while ensuring that no duplicate values are present in the map and a value can be used safely to get the key back.

Interface Declaration

Following is the declaration for com.google.common.collect.Bimap<K,V> interface −

@GwtCompatible
public interface BiMap<K,V>
   extends Map<K,V>

Interface Methods

Sr.No Method & Description
1

V forcePut(K key, V value)

An alternate form of 'put' that silently removes any existing entry with the 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.

Methods Inherited

This class inherits methods from the following interface −

  • java.util.Map

Example of BiMap

Create the following java program using any editor of your choice in say C:/> Guava.

GuavaTester.java

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

public class GuavaTester {

   public static void main(String args[]) {
      BiMap<Integer, String> empIDNameMap = HashBiMap.create();

      empIDNameMap.put(new Integer(101), "Mahesh");
      empIDNameMap.put(new Integer(102), "Sohan");
      empIDNameMap.put(new Integer(103), "Ramesh");

      //Emp Id of Employee "Mahesh"
      System.out.println(empIDNameMap.inverse().get("Mahesh"));
   }	
}

Verify the Result

Compile the class using javac compiler as follows −

C:\Guava>javac GuavaTester.java

Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester

See the result.

101
guava_collections_utilities.htm
Advertisements