Guava's heritage is in the "ancient and unmaintained" Google Collections project and Guava's bidirectional map support comes from that Google Collections heritage. The API documentation for Guava's com.google.common.collect.BiMap interface provides a nice concise definition of a bidirectional map:
A bimap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys. This constraint enables bimaps to support an "inverse view", which is another bimap containing the same entries as this bimap but with reversed keys and values.
I have run into several situations during my career where bidirectional map support is helpful in making clearer and more readable code. I have even built custom implementations of bidirectional maps, but no longer do that thanks to the availability of Guava (and previously of Google Collections and Apache Commons's BidiMap). The next code listing shows a simple situation where a bidirectional map is useful. This example maps nations to their capital cities. The beauty of the bidirectional map is that I can look up a capital city by its nation's name or I can look up a nation's name by the name of the capital city. An important characteristic of the bidirectional map is that the "value" side of the bidirectional map requires unique values in addition to the more typical "key" side of the bidirectional map requiring unique values.
Read full article from Inspired by Actual Events: Guava's Bidirectional Maps
No comments:
Post a Comment