Commons Collections - BidiMap Interface


Advertisements

New Interfaces are added to supports bidirectional Map. Using bidirectional map, a key can be lookup using value and value can be lookup using key easily.

Interface Declaration

Following is the declaration for org.apache.commons.collections4.BidiMap<K,V> interface −

public interface BidiMap<K,V>
   extends IterableMap<K,V>

Methods

The methods for the BidiMap Interface are as follows −

Sr.No. Method & Description
1

K getKey(Object value)

Gets the key that is currently mapped to the specified value.

2

BidiMap<V,K> inverseBidiMap()

Gets a view of this map where the keys and values are reversed.

3

V put(K key, V value)

Puts the key-value pair into the map, replacing any previous pair.

4

K removeValue(Object value)

Removes the key-value pair that is currently mapped to the specified value (optional operation).

5

Set<V> values()

Returns a Set view of the values contained in this map.

Methods Inherited

This interface inherits methods from the following interfaces −

  • org.apache.commons.collections4.Get

  • org.apache.commons.collections4.IterableGet

  • org.apache.commons.collections4.Put

  • java.util.Map

Example of BidiMap Interface

An example of BidiMapTester.java is as follows −

import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.TreeBidiMap;
public class BidiMapTester {
   public static void main(String[] args) {
      BidiMap<String, String> bidi = new TreeBidiMap<>();
      bidi.put("One", "1");
      bidi.put("Two", "2");
      bidi.put("Three", "3");
      System.out.println(bidi.get("One"));
      System.out.println(bidi.getKey("1"));
      System.out.println("Original Map: " + bidi);
      bidi.removeValue("1");
      System.out.println("Modified Map: " + bidi);
      BidiMap<String, String> inversedMap = bidi.inverseBidiMap();
      System.out.println("Inversed Map: " + inversedMap);
   }
}

Output

When you run the code, you will see the following output −

1
One
Original Map: {One=1, Three=3, Two=2}
Modified Map: {Three=3, Two=2}
Inversed Map: {2=Two, 3=Three}
Advertisements