Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
This section describes several mini-implementations that can be more convenient and more efficient than the general-purpose implementations when you don't need their full power. All the implementations in this section are made available via static factory methods rather than public classes.
TheArrays.asList
method returns a List view of its array argument. Changes to the List write through to the array and vice versa. The size of the collection is that of the array and cannot be changed. If the add or the remove method is called on the List, an UnsupportedOperationException will result.The normal use of this implementation is as a bridge between array-based and collection-based APIs. It allows you to pass an array to a method expecting a Collection or a List. However, this implementation also has another use. If you need a fixed-size List, it's more efficient than any general-purpose List implementation. Here's the idiom:
Note that a reference to the backing array is not retained.List<String> list = Arrays.asList(new String[size]);
Occasionally you'll need an immutableList
consisting of multiple copies of the same element. TheCollections.nCopies
method returns such a List. This implementation has two main uses. The first is to initialize a newly created List. For example, suppose that you want an ArrayList initially consisting of 1,000 null elements. The following incantation does the trick:Of course, the initial value of each element needn't be null. The second main use is to grow an existing List. For example, suppose that you want to add 69 copies of the string "fruit bat" to the end of a List<String>. It's not clear why you'd want to do such a thing, but let's just suppose you did. Here's how you'd do it:List<Type> list = new ArrayList<Type>(Collections.nCopies(1000, (Type)null);By using the form of addAll that takes both an index and a Collection, you can add the new elements to the middle of a List instead of the end.lovablePets.addAll(Collections.nCopies(69, "fruit bat"));
Sometimes you'll need an immutable singletonSet
, which consists of a single, specified element. TheCollections.singleton
method returns such aSet
. One use of this implementation is to remove all occurrences of a specified element from a Collection:A related idiom removes from a Map all elements that map to a specified value. For example, suppose that you have a Map, job, that maps people to their line of work. Suppose that you want to eliminate all the lawyers. This one-liner will do the deed:c.removeAll(Collections.singleton(e));One more use of this implementation is to provide a single input value to a method that is written to accept a Collection of values.job.values().removeAll(Collections.singleton(LAWYER));
The Collections class provides methods to return the empty set, list, and map: emptySet, emptyList, and emptyMap. The main use of these constants is as input to methods that take a Collection of values, when you don't want to provide any values at all, for example:tourist.declarePurchases(Collections.emptySet());
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.