This section describes several mini-implementations that can be more
convenient and more efficient than 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.asListmethod returns aListview of its array argument. Changes to theListwrite through to the array and vice versa. The size of the collection is that of the array and cannot be changed. If theaddor theremovemethod is called on theList, anUnsupportedOperationExceptionwill 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
Collectionor aList. However, this implementation also has another use. If you need a fixed-sizeList, it's more efficient than any general-purposeListimplementation. This is 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 immutableListconsisting of multiple copies of the same element. TheCollections.nCopiesmethod returns such a list. This implementation has two main uses. The first is to initialize a newly createdList; for example, suppose you want anArrayListinitially consisting of 1,000nullelements. The following incantation does the trick.Of course, the initial value of each element need not beList<Type> list = new ArrayList<Type>(Collections.nCopies(1000, (Type)null);null. The second main use is to grow an existingList. For example, suppose you want to add 69 copies of the string"fruit bat"to the end of aList<String>. It's not clear why you'd want to do such a thing, but let's just suppose you did. The following is how you'd do it.By using the form oflovablePets.addAll(Collections.nCopies(69, "fruit bat"));addAllthat takes both an index and aCollection, you can add the new elements to the middle of aListinstead of to the end of it.
Sometimes you'll need an immutable singletonSet, which consists of a single, specified element. TheCollections.singletonmethod returns such aSet. One use of this implementation is to remove all occurrences of a specified element from aCollection.A related idiom removes all elements that map to a specified value from ac.removeAll(Collections.singleton(e));Map. For example, suppose you have aMapjob that maps people to their line of work and suppose you want to eliminate all the lawyers. The following one-liner will do the deed.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));
TheCollectionsclass provides methods to return the emptySet,List, andMapemptySet,emptyList, andemptyMap. The main use of these constants is as input to methods that take aCollectionof values when you don't want to provide any values at all, as in this example.tourist.declarePurchases(Collections.emptySet());