📜  共同收藏-BidiMap界面

📅  最后修改于: 2020-11-18 08:22:20             🧑  作者: Mango


添加了新接口以支持双向地图。使用双向映射,可以轻松地使用值查找键和使用键来查找值。

接口声明

以下是org.apache.commons.collections4.BidiMap 接口的声明-

public interface BidiMap
   extends IterableMap

方法

BidiMap接口的方法如下-

Sr.No. Method & Description
1

K getKey(Object value)

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

2

BidiMap 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 values()

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

继承的方法

该接口从以下接口继承方法-

  • org.apache.commons.collections4.Get

  • org.apache.commons.collections4.IterableGet

  • org.apache.commons.collections4.Put

  • java.util.Map

BidiMap界面示例

BidiMapTester.java的示例如下-

import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.TreeBidiMap;
public class BidiMapTester {
   public static void main(String[] args) {
      BidiMap 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 inversedMap = bidi.inverseBidiMap();
      System.out.println("Inversed Map: " + inversedMap);
   }
}

输出

运行代码时,您将看到以下输出-

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