Guava - Bimap Interface

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<K,V> interface −

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

Interface Methods

Sr.No Method & Description

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.


BiMap<V,K> inverse()

Returns the inverse view of this bimap, which maps each of this bimap's values to its associated key.


V put(K key, V value)

Associates the specified value with the specified key in this map (optional operation).


void putAll(Map<? extends K,? extends V> map)

Copies all of the mappings from the specified map to this map (optional operation).


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.


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"

Verify the Result

Compile the class using javac compiler as follows −


Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester

See the result.

Kickstart Your Career

Get certified by completing the course

Get Started