How to create bi-directional map in java

I need a data structure to store pairs of string-int values ​​in a 1: 1 ratio and the ability to look too much for them in the reverse order.

I wrote a class with an Hashtable and String array and saved the data twice and used the built-in functions to search.

My question is, is there a better way to achieve this? And I better understand that I am efficient and did not store data 2 times, and, preferably, I do not write tons of code: P.

+54
java data-structures
Aug 07 2018-10-10T00:
source share
6 answers

It seems that you can search for bitumen.

Google Collections (now part of Guava ) contains BiMap with several implementations.

From the BiMap documentation:

A bimap (or "bi-directional map") is a display that preserves the uniqueness of its value, as well as the keys. This limitation allows a bimap to support a "reverse lookup", which is a different bimap containing the same as this bimap, but with reverse keys and values.

The BiMap.inverse method returns a Map with values ​​as keys and keys as values, so Map can be used to call get on a value and get the key.

In addition, the Map returned by inverse is a representation of the underlying data, so it does not need to make additional copies of the original data.

From the documentation of the BiMap.inverse method:

Returns the inverse of this bimap, which maps each of these bimaps to the corresponding key. Two bimaps are supported by the same data; Any changes to one will be displayed in the other.

+51
Aug 07 '10 at 11:10
source share

You can do a simple implementation as follows. Please note that data is not copied in this implementation. Only links! I added an implementation to add and receive. delete and another necessary method left as an exercise :)

 public class TwoWayHashmap<K extends Object, V extends Object> { private Map<K,V> forward = new Hashtable<K, V>(); private Map<V,K> backward = new Hashtable<V, K>(); public synchronized void add(K key, V value) { forward.put(key, value); backward.put(value, key); } public synchronized V getForward(K key) { return forward.get(key); } public synchronized K getBackward(V key) { return backward.get(key); } } 

And, of course, his responsibility to apply even β€œvalues” is unique. Usage example:

 TwoWayHashmap twmap = new TwoWayHashmap<String, String>(); twmap.add("aaa", "bbb"); twmap.add("xxx", "yyy"); System.out.println(twmap.getForward("xxx")); System.out.println(twmap.getBackward("bbb")); 
+27
Aug 07 '10 at 11:20
source share

Apache Commons also includes BidiMap (bidirectional map).

Defines a map that allows bidirectional searches between a key and values.

This extended Map is a mapping in which a key can search for a value and a value can search for a key with equal ease. This interface extends the Map and therefore can be used wherever a map is required. The interface provides a reverse map display, providing full access to both directions from BidiMap.

+11
May 24 '12 at 15:38
source share

The Google Collections Framework has BiMap does what you want.

+4
Aug 07 2018-10-11T00:
source share

Using Guava ,

  HashBiMap<String, String> map = HashBiMap.create(); map.put("name", "Sohail"); map.put("country", "Pakistan"); Log.d("tag", "name is " + map.get("name")); BiMap<String, String>invmap= map.inverse(); Log.d("tag", "Pakistan is a " + invmap.get("Pakistan")); 

read the full tutorial here.

+3
Mar 29 '13 at 4:56
source share

Create a hash map that maps Object to Object - then you can use the same map to store String β†’ Integer and Integer β†’ String.

When adding a string / int pair, just add it to both paths to the same map.

-four
Aug 07 '10 at 11:11
source share



All Articles