[ Pobierz całość w formacie PDF ]
public static final List EMPTY_LIST
public static final Map EMPTY_MAP
public static final Set EMPTY_SET
These are useful when methods require a collection argument but you have nothing to put in it, and when you
want to ensure that it remains empty.
Think of each of these implementations as having called the default constructor for one of the concrete
collection implementations, and then having called the appropriate unmodifiableXXX() method to ensure the
collection doesn't change. You'll learn more about the unmodifiableXXX() methods shortly in the
"Read-Only Collections" section. For instance, the following would act similar to the EMPTY_LIST defined
above:
List emptyList = Collections.unmodifiableList(new LinkedList());
There are two benefits to using the constants over creating the implementations yourself. The key difference is
that the implementations behind the constants have been optimized with the knowledge that they are empty.
Creating the implementations yourself and making them unmodifiable requires the creation of necessary
internal data structures, even though they will never be used. A secondary benefit of using the empty
collection constants is that you can share the same empty implementation with anyone else without having to
create extra object instances.
Note One nice thing about working with the EMPTY_MAP implementation is that even the collections that the
map methods return are of the EMPTY_SET variety (entrySet(), keySet(), and values()).
Singleton Collections
There exists a trio of methods for creating single-element collections, which act similar to the specialized
methods for creating empty collections:
public static List singletonList(Object element)
public static Set singleton(Object element)
public static Map singletonMap(Object key, Object value)
159
Wrapped Collections
Note Both singletonList() and singletonMap() were added with the 1.3 release of the Java 2 platform.
These are useful when you have a single element and when a method you need to call requires a collection,
not an element.
Like the empty collections, an attempt to modify the collection causes an UnsupportedOperationException to
be thrown. When trying to add a single element or check for its pre-existence in a collection, there is no
difference in calling methods like add(elementOfSingletonCollection) versus addAll(singletonCollection), or
contains(elementOfSingletonCollection) versus containsAll(singletonCollection).
There is, however, a big difference between remove(elementOfSingletonCollection) and
removeAll(singletonCollection). With remove(elementOfSingletonCollection), only the first instance of the
element is removed from the collection. However, with removeAll(singletonCollection), all instances of the
element will be removed. Figure 12-1 should help you visualize this difference.
Figure 12-1: The remove(element) versus the removeAll(singletonCollection).
Wrapped Collections
The Collections class provides two sets of wrapper methods that decorate the underlying collections. The first
set of wrappers allows you to create an unmodifiable or read-only collection. The second set allows you to
create a synchronized or thread-safe collection.
Read-Only Collections
When working with collections, there are times when you need, or at least prefer, unmodifiable access to your
collection instance. This may be because you are finished adding and removing elements from the collection
but you still need to use the collection, or because you need to pass your collection to someone you don't
necessarily know. Or perhaps you know them, but you don't want them to change anything. To ensure that
your collections will not be modified, the Collections class provides a set of six methods to create read-only
instances one for each Collection, List, Map, Set, SortedMap, and SortedSet interface:
public static Collection unmodifiableCollection(Collection c)
public static List unmodifiableList(List l)
public static Map unmodifiableMap(Map m)
public static Set unmodifiableSet(Set s)
public static SortedMap unmodifiableSortedMap(SortedMap m)
public static SortedSet unmodifiableSortedSet(SortedSet s)
[ Pobierz całość w formacie PDF ]