Commons Collections 简明教程
Commons Collections - BidiMap Interface
新接口被添加以支持双向 Map。使用双向 Map,可以使用值来查找键,可以使用键来查找值。
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
下面是对 org.apache.commons.collections4.BidiMap<K,V> 接口的声明 −
Following is the declaration for org.apache.commons.collections4.BidiMap<K,V> interface −
public interface BidiMap<K,V>
extends IterableMap<K,V>
Methods
BidiMap 接口的方法如下 −
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.Ge.
-
org.apache.commons.collections4.IterableGe.
-
org.apache.commons.collections4.Pu.
-
java.util.Ma.
Example of BidiMap Interface
BidiMapTester.java 的示例如下 −
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);
}
}