public static final ImmutableSet<String> COLOR_NAMES = ImmutableSet.of( "red", "orange", "yellow", "green", "blue", "purple"); class Foo { final ImmutableSet<Bar> bars; Foo(Set<Bar> bars) { this.bars = ImmutableSet.copyOf(bars); // defensive copy! } }
Immutable objects have many advantages, including:
Making immutable copies of objects is a good defensive programming technique. Guava provides simple, easy-to-use immutable versions of each standard Collection
type, including Guava's own Collection
variations.
The JDK provides Collections.unmodifiableXXX
methods, but in our opinion, these can be
When you don't expect to modify a collection, or expect a collection to remain constant, it's a good practice to defensively copy it into an immutable collection.
Important: Each of the Guava immutable collection implementations rejects null values. We did an exhaustive study on Google's internal code base that indicated that null
elements were allowed in collections about 5% of the time, and the other 95% of cases were best served by failing fast on nulls. If you need to use null values, consider using Collections.unmodifiableList
and its friends on a collection implementation that permits null. More detailed suggestions can be found here.
An ImmutableXXX
collection can be created in several ways:
copyOf
method, for example, ImmutableSet.copyOf(set)
of
method, for example, ImmutableSet.of("a", "b", "c")
or ImmutableMap.of("a", 1, "b", 2)
Builder
, for example,public static final ImmutableSet<Color> GOOGLE_COLORS = ImmutableSet.<Color>builder() .addAll(WEBSAFE_COLORS) .add(new Color(0, 191, 255)) .build();
Except for sorted collections, order is preserved from construction time. For example,
ImmutableSet.of("a", "b", "c", "a", "d", "b")
will iterate over its elements in the order "a", "b", "c", "d".
copyOf
is smarter than you think
It is useful to remember that ImmutableXXX.copyOf
attempts to avoid copying the data when it is safe to do so -- the exact details are unspecified, but the implementation is typically "smart". For example,
ImmutableSet<String> foobar = ImmutableSet.of("foo", "bar", "baz"); thingamajig(foobar); void thingamajig(Collection<String> collection) { ImmutableList<String> defensiveCopy = ImmutableList.copyOf(collection); ... }
In this code, ImmutableList.copyOf(foobar)
will be smart enough to just return foobar.asList()
, which is a constant-time view of the ImmutableSet
.
As a general heuristic, ImmutableXXX.copyOf(ImmutableCollection)
tries to avoid a linear-time copy if
ImmutableSet.copyOf(ImmutableList)
can't be done in constant time.ImmutableList<String> hugeList
, and you do ImmutableList.copyOf(hugeList.subList(0, 10))
, an explicit copy is performed, so as to avoid accidentally holding on to references in hugeList
that aren't needed.ImmutableSet.copyOf(myImmutableSortedSet)
will perform an explicit copy, because the hashCode()
and equals
used by ImmutableSet
have different semantics from the comparator-based behavior of ImmutableSortedSet
.This helps minimize the performance overhead of good defensive programming style.
All immutable collections provide an ImmutableList
view via asList()
, so -- for example -- even if you have data stored as an ImmutableSortedSet
, you can get the k
th smallest element with sortedSet.asList().get(k)
.
The returned ImmutableList
is frequently -- not always, but frequently -- a constant-overhead view, rather than an explicit copy. That said, it's often smarter than your average List
-- for example, it'll use the efficient contains
methods of the backing collection.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3