Program to Implement a Bidirectional Map Using Two HashSets in Java
Below is the code implementation to implement a bidirectional map using two HashSets.
Java
// Java Program to implement a bidirectional map using two HashSets import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class BidirectionalMap<K, V> { private final Map<K, V> keyToValueMap = new HashMap<>(); private final Map<V, K> valueToKeyMap = new HashMap<>(); // method to put a key-value pair into the bidirectional map public void put(K key, V value) { keyToValueMap.put(key, value); valueToKeyMap.put(value, key); } // method to get a value based on the key public V getValueByKey(K key) { return keyToValueMap.get(key); } // method to get a key based on the value public K getKeyByValue(V value) { return valueToKeyMap.get(value); } // method to check if a key exists in the map public boolean containsKey(K key) { return keyToValueMap.containsKey(key); } // method to check if a value exists in the map public boolean containsValue(V value) { return valueToKeyMap.containsKey(value); } // method to remove a key-value pair based on the key public void removeByKey(K key) { V value = keyToValueMap.remove(key); valueToKeyMap.remove(value); } // method to remove all key-value pairs from the bidirectional map public void removeAll() { keyToValueMap.clear(); valueToKeyMap.clear(); } // method to get a set of all keys in the bidirectional map public Set<K> getAllKeys() { return keyToValueMap.keySet(); } // method to get a set of all values in the bidirectional map public Set<V> getAllValues() { return valueToKeyMap.keySet(); } // Main method to demonstrate the usage of the BidirectionalMap class public static void main(String[] args) { // Create a BidirectionalMap with Integer keys and String values BidirectionalMap<Integer, String> biMap = new BidirectionalMap<>(); // Add key-value pairs biMap.put( 1 , "One" ); biMap.put( 2 , "Two" ); biMap.put( 3 , "Three" ); // Retrieve values by key System.out.println( "Value for key 2: " + biMap.getValueByKey( 2 )); // Retrieve keys by value System.out.println( "Key for value 'Three': " + biMap.getKeyByValue( "Three" )); // checks if a key or value exists System.out.println( "Contains key 4: " + biMap.containsKey( 4 )); System.out.println( "Contains value 'One': " + biMap.containsValue( "One" )); // Remove a key-value pair biMap.removeByKey( 1 ); // Display all keys and values System.out.println( "All keys: " + biMap.getAllKeys()); System.out.println( "All values: " + biMap.getAllValues()); // Clear the map biMap.removeAll(); // checks if the map is empty after clearing System.out.println( "Is the map empty? " + (biMap.getAllKeys().isEmpty() && biMap.getAllValues().isEmpty())); } } |
Output
Value for key 2: Two Key for value 'Three': 3 Contains key 4: false Contains value 'One': true All keys: [2, 3] All values: [Two, Three] Is the map empty? true
Explanation of the Program:
- The above Java program implements a Bidirectional Map. It allows mapping between keys and values in both directions.
- It uses two separate HashMaps for efficient bidirectional lookups.
- The BidirectionalMap class provides methods for putting key-value pairs, retrieving values by key, retrieving keys by value, checking if a key or value exists, removing key-value pairs, and accessing all keys and values.
- The main method demonstrates the usage of this BidirectionalMap with Integer keys and String values.
- It shows various operations such as adding pairs, retrieving values and keys, checking existence, removing pairs, displaying all keys and values, and clearing the map.
How to Implement a Bidirectional Map Using Two HashSets in Java?
Bidirectional maps are also known as two-way maps or dual maps. These provide a convenient way to establish a relationship between keys and values in both directions. In Java, we can implement these using HashSet.
So, in this article, we will see how to implement bidirectional maps in Java using two Hash Sets.
Bidirectional Map Implementation:
A bidirectional map created a relation where each element in one set corresponds to a unique element in another set. This article explores the implementation of bidirectional maps in Java using HashSet collections of Java. The main idea is to maintain two Maps. First for the forward direction and second for the reverse direction.