Bimap provides a new collection type that provides a bidirectional associative data structure for key and value.
Normally, when we use a Java map, we tend to look for value by key, but we need to write some extra code if the scenario below is present. First, consider the following map structure, which represents the identification number and file name.
@Test public void Logmaptest () {map<integer,string> logfilemap = Maps.newhashmap (); Logfilemap.put (1, "A.log"); Logfilemap.put (2, "B.log"); Logfilemap.put (3, "C.log"); System.out.println ("Logfilemap:" +logfilemap); }
When we need to find the file name by ordinal, it's simple. But if we need to find its ordinal by file name, we have to traverse the map. Of course, we can also write a map reversal method to help implement an inverted mapping relationship.
/** * Reverse Map key and value * @param <S> * @param <T> * @param map * @return */ public static <s,t> map<t,s> getinversemap (Map<S,T >&NBSP;MAP) { map<t,s> inversemap = new HashMap<T,S> (); for (entry<s,t> Entry: map.entryset ()) { Inversemap.put (Entry.getvalue (), entry.getkey ()); } return inversemap; }
@Test public void logmaptest () { map<integer,string> logfilemap = maps.newhashmap (); logfilemap.put (1, "A.log"); logfilemap.put (2, "B.log"); logfilemap.put (3, "C.log"); System.out.println ("Logfilemap:" +logfilemap); Map<String,Integer> logfileInverseMap = Maps.newhashmap (); logfileinversemap=getinversemap (Logfilemap); system.out.println("Logfileinversemap:" +logfileinversemap); }
The above code can help us implement the map reversal requirements, but there are some issues we need to consider:
1. How to handle the case of duplicate value. If you don't think about it, there will be coverage when you reverse it.
2. If you add a new key to the inverted map, does the previous map need to update a value?
In this case, things that need to be considered outside the business are increased, and the code you write becomes less readable. Then we can consider using the bimap in guava.
Bimap
Bimap is very simple to use, for the above use of the scenario, we can use a very simple code to achieve:
@Test public void Bimaptest () {bimap<integer,string> logfilemap = Hashbimap.create (); Logfilemap.put (1, "A.log"); Logfilemap.put (2, "B.log"); Logfilemap.put (3, "C.log"); System.out.println ("Logfilemap:" +logfilemap); bimap<string,integer> Filelogmap = Logfilemap.inverse (); System.out.println ("Filelogmap:" +filelogmap); }
Mandatory uniqueness of BIMAP data
When using Bimap, value uniqueness is required. If value is repeated, an error is thrown: java.lang.IllegalArgumentException, for example:
@Test public void Bimaptest () {bimap<integer,string> logfilemap = Hashbimap.create (); Logfilemap.put (1, "A.log"); Logfilemap.put (2, "B.log"); Logfilemap.put (3, "C.log"); Logfilemap.put (4, "D.log"); Logfilemap.put (5, "D.log"); }
Logfilemap.put (5, "D.log") throws Java.lang.IllegalArgumentException:value already present:d.log error. If we do need to insert a duplicate value value, you can choose the Forceput method. But we need to be aware that the previous key will be overwritten.
@Test public void Bimaptest () {bimap<integer,string> logfilemap = Hashbimap.create (); Logfilemap.put (1, "A.log"); Logfilemap.put (2, "B.log"); Logfilemap.put (3, "C.log"); Logfilemap.put (4, "D.log"); Logfilemap.forceput (5, "D.log"); System.out.println ("Logfilemap:" +logfilemap); } output: Logfilemap:{5=d.log, 3=c.log, 2=b.log, 1=a.log}
The
Understanding Inverse Method
Inverse method returns a reversed bimap, but note that this inverted map is not a new map object, it implements a view association, This will affect the original map object for all operations on the inverted map. For example:
@Test public void bimaptest () { bimap<integer,string> logfilemap = hashbimap.create (); logfilemap.put (1, "A.log"); logfilemap.put (2, "B.log"); logfilemap.put (3, " C.log "); system.out.println (" Logfilemap: "+logfileMap); BiMap<String,Integer> filelogMap = Logfilemap.inverse (); system.out.println ("FilelogMap:" + FILELOGMAP); Logfilemap.put (4, "D.log"); system.out.println ("LogfileMap : "+logfilemap); system.out.println ("Filelogmap:" +filelogmap); }
Output:
Logfilemap:{3=c.log, 2=b.log, 1=a.log}filelogmap:{c.log=3, b.log=2, A.log=1}logfilemap:{4=d.log, 3=c.log, 2=b.log, 1 = A.log}filelogmap:{d.log=4, C.log=3, b.log=2, a.log=1}
Implementation class of Bimap
key-value map Impl value-key map Impl corresponding BiMap
HashMap HashMap Hashbimap
Immutablemap Immutablemap Immutablebimap
Enummap Enummap Enumbimap
Enummap HashMap Enumhashbimap
Guava Study notes: Guava new collection type-bimap