- Commons Collections - Discussion
- Commons Collections - Useful Resources
- Commons Collections - Quick Guide
- Commons Collections - Union
- Commons Collections - Subtraction
- Commons Collections - Intersection
- Commons Collections - Inclusion
- Commons Collections - Safe Empty Checks
- Commons Collections - Filtering Objects
- Commons Collections - Transforming Objects
- Commons Collections - Merge & Sort
- Commons Collections - Ignore Null
- Commons Collections - OrderedMap Interface
- Commons Collections - MapIterator Interface
- Commons Collections - BidiMap Interface
- Commons Collections - Bag Interface
- Commons Collections - Environment Setup
- Commons Collections - Overview
- Commons Collections - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Commons Collections - BidiMap Interface
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 −
pubpc 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.Ge.
org.apache.commons.collections4.IterableGe.
org.apache.commons.collections4.Pu.
java.util.Ma.
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; pubpc class BidiMapTester { pubpc 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