Using Guava’s Advanced Map Implementations: Table, BiMap, Multimap, RangeMap, and ClassToInstanceMap
This article introduces Guava’s extended map utilities—including Table, BiMap, Multimap, RangeMap, and ClassToInstanceMap—explaining their concepts, showing how to add the Maven dependency, and providing practical Java code examples that simplify data handling compared to standard JDK maps.
Guava, a Google‑maintained Java library, offers a rich set of APIs that extend the standard JDK collections, cache, concurrency, and I/O utilities, allowing developers to write more concise and efficient code.
After adding the Maven dependency
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>the article demonstrates several Guava map extensions.
Table – Two‑Key Map
Unlike a regular Map that holds a single key‑value pair, Guava’s Table allows one value to be associated with two keys (rowKey and columnKey). A simple example shows how to replace a nested Map<String, Map<String, Integer>> with a Table<String, String, Integer> for storing employee work days per month.
Table<String,String,Integer> table = HashBasedTable.create();
// put elements
table.put("Hydra", "Jan", 20);
table.put("Hydra", "Feb", 28);
// retrieve
Integer dayCount = table.get("Hydra", "Feb");Additional operations include retrieving row/column key sets, values, summing values per row, transposing rows and columns with Tables.transpose , and converting the table back to nested maps via rowMap() or columnMap() .
BiMap – Bidirectional Map
A regular Map requires iteration to find a key by value. Guava’s BiMap provides a one‑to‑one bidirectional mapping, enabling direct lookup in both directions via inverse() . The article shows basic usage, the effect of modifying the inverse view, the restriction that values must be unique, and how to force a replacement with forcePut .
HashBiMap<String, String> biMap = HashBiMap.create();
biMap.put("Hydra", "Programmer");
biMap.put("Tony", "IronMan");
System.out.println(biMap.get("Tony")); // IronMan
BiMap<String, String> inverse = biMap.inverse();
System.out.println(inverse.get("Titan")); // Thanos
inverse.put("IronMan", "Stark"); // modifies original biMapMultimap – Map with Multiple Values per Key
Standard Map stores a single value per key, requiring a collection as the value for multiple entries. Guava’s Multimap abstracts this pattern, allowing direct put and get operations that return a live collection view. The article covers retrieving collections, the difference between ArrayListMultimap and HashMultimap , mutating the returned collection, and converting a multimap to a regular map with asMap() .
Multimap<String, Integer> multimap = ArrayListMultimap.create();
multimap.put("day", 1);
multimap.put("day", 2);
multimap.put("month", 3);
Collection<Integer> dayValues = multimap.get("day");
System.out.println(multimap); // {day=[1, 2], month=[3]}RangeMap – Mapping Intervals to Values
RangeMap maps numeric ranges to values, eliminating verbose if‑else chains. The example creates a TreeRangeMap to classify exam scores into "fail", "satisfactory", and "excellent" using closed/open interval specifications, demonstrates lookups, and shows how removing a range yields null for queries inside the removed interval.
RangeMap<Integer, String> rangeMap = TreeRangeMap.create();
rangeMap.put(Range.closedOpen(0, 60), "fail");
rangeMap.put(Range.closed(60, 90), "satisfactory");
rangeMap.put(Range.openClosed(90, 100), "excellent");
System.out.println(rangeMap.get(59)); // fail
System.out.println(rangeMap.get(75)); // satisfactory
rangeMap.remove(Range.closed(70, 80));
System.out.println(rangeMap.get(75)); // nullClassToInstanceMap – Map from Class to Its Instance
This specialized map uses a Class object as the key and stores an instance of that class as the value. It eliminates manual casting and provides compile‑time type safety via generics. The article shows how to store and retrieve objects, the benefit of type constraints, and contrasts it with a plain Map<Class, Object> .
ClassToInstanceMap<Object> instanceMap = MutableClassToInstanceMap.create();
User user = new User("Hydra", 18);
instanceMap.putInstance(User.class, user);
User retrieved = instanceMap.getInstance(User.class);
System.out.println(user == retrieved); // trueIn summary, Guava’s five map extensions—Table, BiMap, Multimap, RangeMap, and ClassToInstanceMap—provide powerful, concise ways to handle complex key‑value relationships, but developers should be aware of view‑backed behavior and uniqueness constraints to avoid subtle bugs.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.