Guava Immutable Collections and Advanced Collection Types Overview
This article explains why immutable collections improve safety and performance, how to create them with Guava's copyOf, of, and Builder methods, and introduces Guava's extended collection types such as Multiset, Multimap, BiMap, Table, ClassToInstanceMap, RangeSet, and RangeMap with usage examples.
Guava extends the JDK collection framework with mature, immutable collection implementations that support defensive programming and performance gains.
Why use immutable collections? They are thread‑safe, prevent accidental modification by untrusted code, use memory more efficiently, and can serve as constant values. Unlike Collections.unmodifiableXXX , Guava's immutable collections avoid the overhead of wrapping mutable collections.
Creating immutable collections can be done via ImmutableSet.copyOf(set) , ImmutableSet.of("a", "b", "c") , or a Builder such as ImmutableSet.<Color>builder().addAll(WEBSAFE_COLORS).add(new Color(0,191,255)).build() . For ordered immutable sets, sorting occurs at construction, e.g., ImmutableSortedSet.of("a", "b", "c", "a", "d", "b") results in a, b, c, d .
The copyOf method intelligently avoids copying when possible, and all immutable collections provide an asList() view that offers a stable, efficient list representation.
New collection types include:
Multiset : Allows duplicate elements, behaving like an ArrayList<E> or a Map<E,Integer> . Operations such as add(E) , count(Object) , and elementSet() are provided, with implementations like HashMultiset and TreeMultiset .
Multimap : Maps a key to multiple values, simplifying Map<K, List<V>> patterns. Methods include put(K,V) , remove(K,V) , and views like asMap() , entries() , keySet() , and values() . Implementations include ArrayListMultimap , HashMultimap , and immutable variants.
BiMap : A bidirectional map where values are unique; supports inverse() and forcePut(key,value) . Implementations include HashBiMap , ImmutableBiMap , and EnumBiMap .
Table : Represents a two‑dimensional map with row and column keys, offering views such as rowMap() , columnMap() , and cellSet() . Implementations include HashBasedTable , TreeBasedTable , ImmutableTable , and ArrayTable .
ClassToInstanceMap : Maps a Class<? extends B> to an instance of that type, providing type‑safe getInstance and putInstance methods. Implementations are MutableClassToInstanceMap and ImmutableClassToInstanceMap .
RangeSet : Represents a set of non‑overlapping, non‑empty intervals. Example usage: RangeSet<Integer> rangeSet = TreeRangeSet.create(); rangeSet.add(Range.closed(1,10)); // {[1,10]} rangeSet.add(Range.closedOpen(11,15)); // {[1,10], [11,15)} rangeSet.add(Range.closedOpen(15,20)); // {[1,10], [11,20)} rangeSet.remove(Range.open(5,10)); // {[1,5], [10,10], [11,20)}
RangeMap : Maps non‑overlapping intervals to values. Example usage: RangeMap<Integer,String> rangeMap = TreeRangeMap.create(); rangeMap.put(Range.closed(1,10), "foo"); rangeMap.put(Range.open(3,6), "bar"); // results in {[1,3]=>"foo", (3,6)=>"bar", [6,10]=>"foo"} rangeMap.remove(Range.closed(5,11)); // {[1,3]=>"foo", (3,5)=>"bar", (11,20)=>"foo"}
All Guava immutable collection implementations reject null values; if null is required, use the JDK's Collections.unmodifiableXXX methods instead.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
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.