BiMap of Guava Learning

Source: Internet
Author: User

In the previous article "Multimap of Guava learning", we talked about Multimap In the Guava class library, which features that key-value pairs in Multimap can be unique. We know that, there is a Map in the Java Collection class library, which features that the stored Key is unique, and the Value can be unique. If we need a Key) each Value is unique. How can this problem be solved? This is the BiMap structure to be discussed today.
In the past, if we had to reverse the key-value pairs in the Map structure (that is, convert key-> value into value-> key), we had to define two Map data structures for storage. However, what happens if multiple elements with the same value exist in the Map? At this time, the added key will overwrite the previously added key. As follows:

K1-> v1, k2-> v2, k3-> v3, k4-> v1 if we need to reverse the key and Value, it will become v1-> k4, v2-> k2, v3-> k3, careful people will find, why is one missing? Because k1-> v1 and k4-> v1 is reversed and then changed to v1-> k1 and v1-> k4, however, because of Map features, v1-> k4 will overwrite previous v1-> k1.

This is where BiMap is used! BiMap <K, V> is the data type of Map <K, V> (because BiMap implements java. util. map interface, pay attention to the difference with Multimap, Multimap does not implement the Map interface ). It features that its value is the same as its key and cannot be repeated. In other words, its key and value are equivalent. If you put repeated elements in the value OF BiMap, you will get IllegalArgumentException. As follows:

BiMap<String, String> upperToSmall = HashBiMap.create();upperToSmall.put("A", "a");upperToSmall.put("B", "b");upperToSmall.put("C", "c");System.out.println(upperToSmall.get("A"));

A unique structure for storing the key and value will be obtained. if you add a line in the above Code

upperToSmall.put("D", "c");

The program will throw the following exception

Exception in thread "main" java. lang. IllegalArgumentException: value already present: c
At com. google. common. collect. HashBiMap. put (HashBiMap. java: 241)
At com. google. common. collect. HashBiMap. put (HashBiMap. java: 218)
At com. wyp. test. testFiles (test. java: 142)
At com. wyp. test. main (test. java: 153)

This is because BiMap enforces uniqueness: BiMap enforces the uniqueness of its value. If a violation is found, it will throw IllegalArgumentException. Isn't it possible to add a value that has already been added? Whether the answer is, we can use BiMap. forcePut (key, value) provided by BiMap. So how can we reverse the key and value through BiMap? The Code is as follows:

BiMap<String, String> smallToUpper = upperToSmall.inverse();System.out.println(smallToUpper.get("a")); //

In this way, the key value is reversed! Note that the inverse method returns a reverse BiMap, that is, the ing of key/value switching. The reversed map is not a new map (upperToSmall = upperToSmall. inverse (). inverse () is a view, which means that any addition, deletion, and modification operations in the reversed map will affect the original map.

Common BiMap implementations include:

  1. HashBiMap: The key set and value set both have HashMap implementation.
  2. EnumBiMap: both key and value must be of the enum type.
  3. ImmutableBiMap: unchangeable BiMap
Reprinted Please note: Reprinted from past memories (http://www.wypblog.com /)
BiMap (http://www.wypblog.com/archives/501) for Guava Learning)

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.