Java Reference
In-Depth Information
public static <T> List<T> emptyList()
Returns an empty, immutable list of the desired type.
public static <T> Set<T> emptySet()
Returns an empty, immutable set of the desired type.
public static <K,V> Map<K,V> emptyMap()
Returns an empty, immutable map of the desired type.
There are also legacy static final fields, EMPTY_LIST , EMPTY_SET , and
EMPTY_MAP , of the raw types List , Set , and Map , initialized to refer to empty
immutable collection objects. The use of these fields is not type-safe and
is discouraged, as is all use of raw types.
21.10.2. The Unmodifiable Wrappers
The Collections class has static methods that return unmodifiable wrap-
pers for all of the collection types: unmodifiableCollection , unmodifi-
ableSet , unmodifiableSortedSet , unmodifiableList , unmodifiableMap , and un-
modifiableSortedMap . The collections returned by these methods pass
non-modifying methods through to the underlying collection. Modifying
methods throw UnsupportedOperationException . The unmodifiable wrapper
is backed by the original collection, so any changes you make to the
collection will be visible through the wrapped collection. In other words,
the contents of an unmodifiable wrapper can change, but not through
the wrapper itself.
Unmodifiable wrappers are a reasonable way to expose information that
may be changing but that shouldn't be changed by the observer. For
example, consider the Attributed interface and the AttributedImpl class
from Chapter 4 . The attributes are exposed by having the attrs method
return an Iterator a reasonable design. An alternative, however, would
 
Search WWH ::




Custom Search