Collections Class
Home ] Up ] Comparable Interface ] Comparator Interface ] [ Collections Class ] Comparable Example ] Comparator Example ] General Applicability ]

 

 

The Collections class (in the java.util package) defines a set of methods and constants that are useful for working with collections and maps.  

Object
    Collections

The Collections class consists only of static methods and constants, and may not be instantiated. It should not be confused with the Collection (singular!) interface.

Collections implements the following public methods:

package java.util;

public class Collections 
{
    // Algorithms

    // sort sorts the specified list into ascending order, 
    // according to the natural ordering of its elements.
    // (uses a modified merge sort)
    public static void sort(List list) {...}
    public static void sort(List list, Comparator c) {...}
    // binarySearch searches the specified list for the specified object 
    // using the binary search algorithm
    public static int binarySearch(List list, Object key) {...}
    public static int binarySearch(List list, Object key, Comparator c) {...}
    // reverse reverses the order of the elements in the specified list.
    public static void reverse(List l) {...}
    // shuffle randomly permutes the specified list
    public static void shuffle(List list) {...}
    public static void shuffle(List list, Random rnd) {...}
    // fill replaces all of the elements of the specified list 
    // with the specified element.
    public static void fill(List list, Object o) {...}
    // copy copies all of the elements from one list into another.
    public static void copy (List dest, List src) {...}
    // min returns the minimum element of the given collection, 
    // according to the natural ordering of its elements.
    public static Object min(Collection coll) {...}
    public static Object min(Collection coll, Comparator comp) {...}
    // max returns the maximum element of the given collection, 
    // according to the natural ordering of its elements.
    public static Object max(Collection coll) {...}
    public static Object max(Collection coll, Comparator comp) {...}

    // Unmodifiable Wrappers

    public static Collection unmodifiableCollection(Collection c) {...}
    public static Set unmodifiableSet(Set s) {...}
    public static SortedSet unmodifiableSortedSet(SortedSet s) {...}
    public static List unmodifiableList(List list) {...}
    public static Map unmodifiableMap(Map m) {...}
    public static SortedMap unmodifiableSortedMap(SortedMap m) {...}

    // Synch Wrappers

    public static Collection synchronizedCollection(Collection c) {...}
    public static Set synchronizedSet(Set s) {...}
    public static SortedSet synchronizedSortedSet(SortedSet s) {...}
    public static List synchronizedList(List list) {...}
    public static Map synchronizedMap(Map m) {...}
    public static SortedMap synchronizedSortedMap(SortedMap m) {...}

    // Miscellaneous

    /**
     * The empty set (immutable).  This set is serializable.
     */
    public static final Set EMPTY_SET = ...;

    /**
     * The empty list (immutable).  This list is serializable.
     */
    public static final List EMPTY_LIST = ...;

    /**
     * Returns an immutable set containing only the specified object.
     * The returned set is serializable.
     *
     * @return an immutable set containing only the specified object.
     */
    public static Set singleton(final Object o) {...}

    /**
     * Returns an immutable list consisting of n copies of the
     * specified object.  This method is useful in combination 
     * with the List.addAll method to grow lists.
     * The returned list is serializable.
     *
    public static List nCopies(final int n, final Object o) {...}

    /**
     * Returns a comparator that imposes the reverse of the natural
     * ordering on a collection of objects that implement the
     * Comparable interface.  (The natural ordering is the ordering
     * imposed by the objects' own compareTo method.)  This enables a
     * simple idiom for sorting (or maintaining) collections (or arrays) of
     * objects that implement the Comparable interface in
     * reverse-natural-order.  For example, suppose a is an array of
     * strings. Then:
     * 		Arrays.sort(a, Collections.reverseOrder());
     * sorts the array in reverse-lexicographic (alphabetical) order.
     *
     * The returned comparator is serializable.
     */
    public static Comparator reverseOrder() {...}

    /**
     * Returns an enumeration over the specified collection.  This provides
     * interoperability with legacy APIs that require an enumeration
     * as input.
     */
    public static Enumeration enumeration(final Collection c) {...}
}

where "..." represents implementation code, the details of which have been omitted.

 

This page was last modified on 02 October, 2007