Collection whose contents will never change, and which offers a few additional
guarantees detailed below.Warning
avoid direct usage of ImmutableCollection as a type (just as
with Collection itself). Prefer subtypes such as ImmutableSet or ImmutableList, which have well-defined equals semantics, thus avoiding a common source
of bugs and confusion.
Immutable- collectionsThe remainder of this documentation applies to every public Immutable- type in this
package, whether it is a subtype of ImmutableCollection or not.
Each makes the following guarantees:
Collections#unmodifiableCollection, whose contents change whenever the wrapped collection
is modified.
ImmutableSortedSet#naturalOrder). See the
appropriate factory method for details. View collections such as ImmutableMultiset#elementSet iterate in the same order as the parent, except as noted.
These are classes instead of interfaces to prevent external subtyping, but should be thought
of as interfaces in every important sense. Each public class such as ImmutableSet is a
type offering meaningful behavioral guarantees. This is substantially different from the
case of (say) HashSet, which is an implementation, with semantics that were
largely defined by its supertype.
For field types and method return types, you should generally use the immutable type (such as
ImmutableList) instead of the general collection interface type (such as List).
This communicates to your callers all of the semantic guarantees listed above, which is almost
always very useful information.
On the other hand, a parameter type of ImmutableList is generally a nuisance to
callers. Instead, accept Iterable and have your method or constructor body pass it to the
appropriate copyOf method itself.
Expressing the immutability guarantee directly in the type that user code references is a
powerful advantage. Although Java offers certain immutable collection factory methods, such as
Collections#singleton(Object) and Set.of,
we recommend using these classes instead for this reason (as well as for consistency).
Except for logically "abstract" types like ImmutableCollection itself, each Immutable type provides the static operations you need to obtain instances of that type. These
usually include:
of, accepting an explicit list of elements or entries.
copyOf (or copyOfSorted), accepting an existing
collection whose contents should be copied.
Builder class which can be used to populate a new immutable
instance.
Object#equals behavior) while it is contained in a
collection. Undefined behavior and bugs will result. It's generally best to avoid using
mutable objects as elements at all, as many users may expect your "immutable" object to be
deeply immutable.
copyOf methods will sometimes recognize that the actual copy operation is
unnecessary; for example, copyOf(copyOf(anArrayList)) should copy the data only
once. This reduces the expense of habitually making defensive copies at API boundaries.
However, the precise conditions for skipping the copy operation are undefined.
ImmutableMap#keySet or ImmutableList#subList may retain a reference to the entire data set, preventing it from
being garbage collected. If some of the data is no longer reachable through other means,
this constitutes a memory leak. Pass the view collection to the appropriate copyOf
method to obtain a correctly-sized copy.
Builder class can be assumed to be no
worse, and possibly better, than creating a mutable collection and copying it.
hashCode implementation, it should cache it itself.
class Foo {
private static final ImmutableSet<String> RESERVED_CODES =
ImmutableSet.of("AZ", "CQ", "ZX");
private final ImmutableSet<String> codes;
public Foo(Iterable<String> codes) {
this.codes = ImmutableSet.copyOf(codes);
checkArgument(Collections.disjoint(this.codes, RESERVED_CODES));
}
}
See the Guava User Guide article on immutable collections.
| Modifier and Type | Class and Description |
|---|---|
| public abstract static class |
| Modifier and Type | Field and Description |
|---|---|
| private static final Object[] | |
| pack-priv static final int |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public final boolean | add(E
element whose presence in this collection is to be ensured e)Overrides java. Implements java.
Deprecated
Unsupported operation.
Guaranteed to throw an exception and leave the collection unmodified.
|
| public final boolean | addAll(Collection<? extends E>
collection containing elements to be added to this collection newElements)Overrides java. Implements java.
Deprecated
Unsupported operation.
Guaranteed to throw an exception and leave the collection unmodified.
|
| public ImmutableList | asList()
Returns an |
| public final void | clear()
Overrides java. Implements java.
Deprecated
Unsupported operation.
Guaranteed to throw an exception and leave the collection unmodified.
|
| public abstract boolean | contains(Object
element whose presence in this collection is to be tested object)Redeclares java. Overrides java. true if this collection contains the specified element.
|
| pack-priv int | copyIntoArray(@Nullable Object[] dst, int offset)
Copies the contents of this immutable collection into the specified array at the specified offset. |
| pack-priv Object[] | internalArray()
If this collection is backed by an array of its elements in insertion order, returns it. |
| pack-priv int | internalArrayEnd()
If this collection is backed by an array of its elements in insertion order, returns the offset where this collection's elements end. |
| pack-priv int | internalArrayStart()
If this collection is backed by an array of its elements in insertion order, returns the offset where this collection's elements start. |
| pack-priv abstract boolean | isPartialView()
Returns |
| public abstract UnmodifiableIterator | iterator()
Redeclares java. Implements abstract java. |
| private void | |
| public final boolean | remove(Object
element to be removed from this collection, if present object)Overrides java. Implements java.
Deprecated
Unsupported operation.
Guaranteed to throw an exception and leave the collection unmodified.
|
| public final boolean | removeAll(Collection<?>
collection containing elements to be removed from this collection oldElements)Overrides java. Implements java.
Deprecated
Unsupported operation.
Guaranteed to throw an exception and leave the collection unmodified.
|
| public final boolean | removeIf(Predicate<? super E>
a predicate which returns filter)true for elements to be
removedOverrides default java.
Deprecated
Unsupported operation.
Guaranteed to throw an exception and leave the collection unmodified.
|
| public final boolean | retainAll(Collection<?>
collection containing elements to be retained in this collection elementsToKeep)Overrides java. Implements java.
Deprecated
Unsupported operation.
Guaranteed to throw an exception and leave the collection unmodified.
|
| public Spliterator | spliterator()
Overrides default java. Spliterator over the elements in this collection.
|
| public final Object[] | toArray()
Overrides java. Implements java. |
| public final <T extends @Nullable Object> T[] | toArray(T[]
the array into which the elements of this collection are to be
stored, if it is big enough; otherwise, a new array of the same
runtime type is allocated for this purpose. other)Overrides java. Implements java. |
| pack-priv Object |
| EMPTY_ARRAY | back to summary |
|---|---|
| private static final Object[] EMPTY_ARRAY | |
| SPLITERATOR_CHARACTERISTICS | back to summary |
|---|---|
| pack-priv static final int SPLITERATOR_CHARACTERISTICS | |
| ImmutableCollection | back to summary |
|---|---|
| pack-priv ImmutableCollection() | |
| add | back to summary |
|---|---|
| public final boolean add(E e) Overrides java. Implements java. Deprecated Unsupported operation. Guaranteed to throw an exception and leave the collection unmodified.
| |
| addAll | back to summary |
|---|---|
| public final boolean addAll(Collection<? extends E> newElements) Overrides java. Implements java. Deprecated Unsupported operation. Guaranteed to throw an exception and leave the collection unmodified.
| |
| asList | back to summary |
|---|---|
| public ImmutableList Returns an Performance note: in most cases this method can return quickly without actually copying anything. The exact circumstances under which the copy is performed are undefined and subject to change.
| |
| clear | back to summary |
|---|---|
| public final void clear() Overrides java. Implements java. Deprecated Unsupported operation. Guaranteed to throw an exception and leave the collection unmodified.
| |
| contains | back to summary |
|---|---|
| public abstract boolean contains(Object object) Redeclares java. Overrides java. Doc from java. Returns
| |
| copyIntoArray | back to summary |
|---|---|
| pack-priv int copyIntoArray(@Nullable Object[] dst, int offset) Copies the contents of this immutable collection into the specified array at the specified
offset. Returns
| |
| internalArray | back to summary |
|---|---|
| pack-priv Object[] internalArray() If this collection is backed by an array of its elements in insertion order, returns it.
| |
| internalArrayEnd | back to summary |
|---|---|
| pack-priv int internalArrayEnd() If this collection is backed by an array of its elements in insertion order, returns the offset where this collection's elements end. | |
| internalArrayStart | back to summary |
|---|---|
| pack-priv int internalArrayStart() If this collection is backed by an array of its elements in insertion order, returns the offset where this collection's elements start. | |
| isPartialView | back to summary |
|---|---|
| pack-priv abstract boolean isPartialView() Returns | |
| iterator | back to summary |
|---|---|
| public abstract UnmodifiableIterator Redeclares java. Implements abstract java. Returns an unmodifiable iterator across the elements in this collection.
| |
| readObject | back to summary |
|---|---|
private void readObject(ObjectInputStream stream) throws InvalidObjectException
| |
| remove | back to summary |
|---|---|
| public final boolean remove(Object object) Overrides java. Implements java. Deprecated Unsupported operation. Guaranteed to throw an exception and leave the collection unmodified.
| |
| removeAll | back to summary |
|---|---|
| public final boolean removeAll(Collection<?> oldElements) Overrides java. Implements java. Deprecated Unsupported operation. Guaranteed to throw an exception and leave the collection unmodified.
| |
| removeIf | back to summary |
|---|---|
| public final boolean removeIf(Predicate<? super E> filter) Overrides default java. Deprecated Unsupported operation. Guaranteed to throw an exception and leave the collection unmodified.
| |
| retainAll | back to summary |
|---|---|
| public final boolean retainAll(Collection<?> elementsToKeep) Overrides java. Implements java. Deprecated Unsupported operation. Guaranteed to throw an exception and leave the collection unmodified.
| |
| spliterator | back to summary |
|---|---|
| public Spliterator Overrides default java. Doc from java. Creates a The default implementation should be overridden by subclasses that
can return a more efficient spliterator. In order to
preserve expected laziness behavior for the
These requirements ensure that streams produced by the
| |
| toArray | back to summary |
|---|---|
| public final Object[] toArray() Overrides java. Implements java. Doc from java. Returns an array containing all of the elements in this collection.
If this collection makes any guarantees as to what order its elements
are returned by its iterator, this method must return the elements in
the same order. The returned array's runtime component type is The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.
| |
| toArray | back to summary |
|---|---|
| public final <T extends @Nullable Object> T[] toArray(T[] other) Overrides java. Implements java. Doc from java. Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection. If this collection fits in the specified array with room to spare
(i.e., the array has more elements than this collection), the element
in the array immediately following the end of the collection is set to
If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
| |
| writeReplace | back to summary |
|---|---|
pack-priv Object writeReplace()
| |
ImmutableCollection types.
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final int |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public abstract ImmutableCollection. | Returns: thisBuilder instancethe element to add element)Adds |
| public ImmutableCollection. | Returns: thisBuilder instancethe elements to add elements)Adds each element of |
| public ImmutableCollection. | |
| public ImmutableCollection. | |
| public abstract ImmutableCollection | build()
Returns a newly-created |
| pack-priv static int |
| DEFAULT_INITIAL_CAPACITY | back to summary |
|---|---|
| pack-priv static final int DEFAULT_INITIAL_CAPACITY | |
| Builder | back to summary |
|---|---|
| pack-priv Builder() | |
| add | back to summary |
|---|---|
| public abstract ImmutableCollection. Adds Note that each builder class covariantly returns its own type from this method.
| |
| add | back to summary |
|---|---|
| public ImmutableCollection. Adds each element of Note that each builder class overrides this method in order to covariantly return its own type.
| |
| addAll | back to summary |
|---|---|
| public ImmutableCollection. Adds each element of Note that each builder class overrides this method in order to covariantly return its own type.
| |
| addAll | back to summary |
|---|---|
| public ImmutableCollection. Adds each element of Note that each builder class overrides this method in order to covariantly return its own type.
| |
| build | back to summary |
|---|---|
| public abstract ImmutableCollection Returns a newly-created Note that each builder class covariantly returns the appropriate type of | |
| expandedCapacity | back to summary |
|---|---|
| pack-priv static int expandedCapacity(int oldCapacity, int minCapacity) | |