Guava Study Notes: Guava adds the set type-Bimap

Source: Internet
Author: User

BiMap provides a new set type, which provides a bidirectional Association of key and value data structures.
In general, when we use Java Map, we usually use the key to find the value. However, in the following scenario, we need to write additional code. First, let's look at the following map structure that indicates the serial number and file name.

 <Integer,String> logfileMap =1,"a.log"2,"b.log"3,"c.log""logfileMap:"+

It is easy to search for a file name by serial number. However, if we need to find the serial number through the file name, we have to traverse the map. Of course, we can also write a Map inversion method to help implement inverted ing relationships.

               <S,T> Map<T,S> getInverseMap(Map<S,T><T,S> inverseMap =  HashMap<T,S>(Entry<S,T>
 <Integer,String> logfileMap =1,"a.log"2,"b.log"3,"c.log""logfileMap:"+<String,Integer> logfileInverseMap =="logfileInverseMap:"+

The above code can help us implement the requirements of map inversion, but there are still some issues we need to consider:
1. How to Handle repeated values. If you do not consider it, the system will overwrite it when you reverse it.
2. If a new key is added to the reverse map, does the map before the reverse change need to update a value?
In this case, the content outside the business needs to be considered is added, and the code written is not so easy to read. Now we can consider using BiMap in Guava.

  Bimap

  Bimap is very simple to use. For the above use cases, we can implement it with simple code:

 <Integer,String> logfileMap =1,"a.log"2,"b.log"3,"c.log""logfileMap:"+<String,Integer> filelogMap ="filelogMap:"+

  Mandatory uniqueness of Bimap data

  When using BiMap, the uniqueness of Value is required. If the value already exists, an error occurs: java. lang. IllegalArgumentException. For example:

 <Integer,String> logfileMap =1,"a.log"2,"b.log"3,"c.log"4,"d.log"5,"d.log"

5, "d. log"

 

 <Integer,String> logfileMap =1,"a.log"2,"b.log"3,"c.log"4,"d.log"5,"d.log""logfileMap:"+5=d.log, 3=c.log, 2=b.log, 1=a.log}

 

Understand the inverse Method
The inverse method returns a reverse BiMap, but note that the reverse map is not a new map object and implements a view Association, in this way, all operations on the reverse map will affect the original map object. For example:

 <Integer,String> logfileMap =1,"a.log"2,"b.log"3,"c.log""logfileMap:"+<String,Integer> filelogMap ="filelogMap:"+4,"d.log""logfileMap:"+"filelogMap:"+

Output:

logfileMap:{3=c.log, 2=b.log, 1==3, b.log=2, a.log=14=d.log, 3=c.log, 2=b.log, 1==4, c.log=3, b.log=2, a.log=1}

  BiMap implementation class

 

Key-Value Map Impl Value-Key Map Impl Corresponding BiMap
HashMap HashBiMap
ImmutableMap ImmutableBiMap
EnumMap EnumBiMap
EnumMap HashMap EnumHashBiMap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.