Collection Utilities and Wrappers

   

Besides the methods already introduced, the Collections class provides a number of other useful support functions. This section outlines some of the more commonly used utilities and the Collections methods that wrap the general-purpose collection implementations with special features.

Utility Methods and Fields

In addition to searching for a specific object in a collection, you can use Collections to locate the maximum or minimum object based either on natural ordering or a Comparator:

 public static Object max(Collection coll) public static Object max(Collection coll, Comparator c) public static Object min(Collection coll) public static Object min(Collection coll, Comparator c) 

You can reorder the elements in a List by reversing or randomly shuffling them:

 public static void reverse(List l) public static void shuffle(List l) public static void shuffle(List l, Random rnd) 

You can copy from one List into another:

 public static void copy(List src, List, dest) 

You can replace each element in a List with a specific object:

 public static void fill(List list, Object o) 

You can create a new List with a specified number of copies of a single object:

 publis static List nCopies(int n, Object o) 

Even when you use the collection framework interfaces and classes, you might need to interface with an API that relies on the Enumeration interface instead of the preferred Iterator. You can create an Enumeration for any Collection:

 public static Enumeration enumeration(Collection c) 

The Collections class defines three publicly accessible fields in addition to its methods. EMPTY_LIST, EMPTY_MAP, and EMPTY_SET represent immutable, empty implementations of the corresponding interfaces. When you need to return an empty collection from a method, for example, you can reference one of these fields and avoid the overhead of creating a new one. EMPTY_MAP was added as part of the Java 1.3 release.

Singletons

Collections provides several methods you can use to create an immutable collection or map that holds one and only one element:

 public static Set singleton(Object o) public static List singletonList(Object o) public static Map singletonMap(Object key, Object value) 

The singleton method was part of the original collections framework, but singletonList and singletonMap were not added until Java 1.3.

Synchronized Collections

The description of the general-purpose implementations of the collections framework interfaces pointed out that these classes, unlike their legacy counterparts, are not thread safe. When you do not require a container to be synchronized, these implementations offer an efficient solution with no extra overhead. However, when you do need synchronization, these classes are not sufficiently capable on their own. Rather than including a second set of classes to provide synchronization, the framework relies on a set of Collections methods that returns a wrapped version of a specified collection that is thread safe:

 public static Collection synchronizedCollection(Collection c) public static Set synchronizedSet(Set s) public static SortedSet synchronizedSortedSet(SortedSet s) public static List synchronizedList(List l) public static Map synchronizedMap(Map m) public static SortedMap synchronizedSortedMap(SortedMap m) 

You must follow two guidelines to use these wrapped collections correctly. First, all access to a collection must be done through the wrapper version returned by one of these methods to guarantee synchronized thread access to the underlying collection. Also, you must manually synchronize the wrapper collection using a synchronized block whenever you iterate through it. See Chapter 11, "Threads," for details.

Unmodifiable Collections

A feature of the general-purpose collection interface implementations is that they support all the optional operations defined by their corresponding interfaces. A result of this is that these collections are modifiable. The Collections class provides wrappers to give you read-only views of a collection if you need to ensure that it is not modified:

 public static Collection unmodifiableCollection(Collection c) public static Set unmodifiableSet(Set s) public static SortedSet unmodifiableSortedSet(SortedSet s) public static List unmodifiableList(List l) public static Map unmodifiableMap(Map m) public static SortedMap unmodifiableSortedMap(SortedMap m) 
   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net