Moyosoft Products | Services | Download | Contact us    
JavaDocPlus
Products  >  JavaDocPlus  >  Online demo    


Latest release




Get a FREE license !
(Special offer, time limited)

Review JavaDocPlus on your blog and we'll give you a free license.

Open source projects licenced under LGPL, EPL, ASL, BSD or MIT can also obtain a free license.

Contact us to check if your project/blog is eligible.

Online demo
JavaDocPlus Demo  |  Show in full page mode
   
Overview  |  
Classic JavaDoc

Class Collections

java.lang.Objectjava.util.Collections


public class Collections

This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends.

The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.

The documentation for the polymorphic algorithms contained in this class generally includes a brief description of the implementation. Such descriptions should be regarded as implementation notes, rather than parts of the specification. Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to. (For example, the algorithm used by sort does not have to be a mergesort, but it does have to be stable.)

The "destructive" algorithms contained in this class, that is, the algorithms that modify the collection on which they operate, are specified to throw UnsupportedOperationException if the collection does not support the appropriate mutation primitive(s), such as the set method. These algorithms may, but are not required to, throw this exception if an invocation would have no effect on the collection. For example, invoking the sort method on an unmodifiable list that is already sorted may or may not throw UnsupportedOperationException.

This class is a member of the Java Collections Framework.

Author:
Josh BlochNeal Gafter

Version:
1.89, 07/28/04

See Also:
Collection, Set, List, Map

Since:
1.2

Fields

static List
EMPTY_LIST
The empty list (immutable). This list is serializable.
static Map
EMPTY_MAP
The empty map (immutable). This map is serializable.
static Set
EMPTY_SET
The empty set (immutable). This set is serializable.

Methods

static boolean
addAll (Collection c, Object a)
Adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster un ... more >
static int
binarySearch (List list, Object key)
Searches the specified list for the specified object using the binary search algorithm. The list must be sorted into ascending order according to the natural ordering of its elements (as by the sort(List) method, above) prior to making this call. If it is not sorted, the results are ... more >
static int
binarySearch (List list, Object key, Comparator c)
Searches the specified list for the specified object using the binary search algorithm. The list must be sorted into ascending order according to the specified comparator (as by the Sort(List, Comparator) method, above), prior to making this call. If it is not sorted, the results are ... more >
static Collection
checkedCollection (Collection c, Class type)
Returns a dynamically typesafe view of the specified collection. Any attempt to insert an element of the wrong type will result in an immediate ClassCastException. Assuming a collection contains no incorrectly typed elements prior to the time a dynamically typesafe view is generated, ... more >
static List
checkedList (List list, Class type)
Returns a dynamically typesafe view of the specified list. Any attempt to insert an element of the wrong type will result in an immediate ClassCastException. Assuming a list contains no incorrectly typed elements prior to the time a dynamically typesafe view is generated, and that all ... more >
static Map
checkedMap (Map m, Class keyType, Class valueType)
Returns a dynamically typesafe view of the specified map. Any attempt to insert a mapping whose key or value have the wrong type will result in an immediate ClassCastException. Similarly, any attempt to modify the value currently associated with a key will result in an immediate Clas ... more >
static Set
checkedSet (Set s, Class type)
Returns a dynamically typesafe view of the specified set. Any attempt to insert an element of the wrong type will result in an immediate ClassCastException. Assuming a set contains no incorrectly typed elements prior to the time a dynamically typesafe view is generated, and that all s ... more >
static SortedMap
checkedSortedMap (SortedMap m, Class keyType, Class valueType)
Returns a dynamically typesafe view of the specified sorted map. Any attempt to insert a mapping whose key or value have the wrong type will result in an immediate ClassCastException. Similarly, any attempt to modify the value currently associated with a key will result in an immedia ... more >
static SortedSet
checkedSortedSet (SortedSet s, Class type)
Returns a dynamically typesafe view of the specified sorted set. Any attempt to insert an element of the wrong type will result in an immediate ClassCastException. Assuming a sorted set contains no incorrectly typed elements prior to the time a dynamically typesafe view is generated, ... more >
static void
copy (List dest, List src)
Copies all of the elements from one list into another. After the operation, the index of each copied element in the destination list will be identical to its index in the source list. The destination list must be at least as long as the source list. If it is longer, the remaining el ... more >
static boolean
disjoint (Collection c1, Collection c2)
Returns true if the two specified collections have no elements in common. Care must be exercised if this method is used on collections that do not comply with the general contract for Collection. Implementations may elect to iterate over either collection and test for containment in ... more >
static List
emptyList ()
Returns the empty list (immutable). This list is serializable. This example illustrates the type-safe way to obtain an empty list: List<String> s = Collections.emptyList(); Implementation note: Implementations of this method need not create a separate List object fo ... more >
static Map
emptyMap ()
Returns the empty map (immutable). This map is serializable. This example illustrates the type-safe way to obtain an empty set: Map<String, Date> s = Collections.emptyMap(); Implementation note: Implementations of this method need not create a separate Map object fo ... more >
static Set
emptySet ()
Returns the empty set (immutable). This set is serializable. Unlike the like-named field, this method is parameterized. This example illustrates the type-safe way to obtain an empty set: Set<String> s = Collections.emptySet(); Implementation note: Implementations of ... more >
enumeration (Collection c)
Returns an enumeration over the specified collection. This provides interoperability with legacy APIs that require an enumeration as input.
static void
fill (List list, Object obj)
Replaces all of the elements of the specified list with the specified element. This method runs in linear time.
static int
frequency (Collection c, Object o)
Returns the number of elements in the specified collection equal to the specified object. More formally, returns the number of elements e in the collection such that (o == null ? e == null : o.equals(e)).
static int
indexOfSubList (List source, List target)
Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence. More formally, returns the lowest index i such that source.subList(i, i+target.size()).equals(target), or -1 if there is no such i ... more >
static int
lastIndexOfSubList (List source, List target)
Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence. More formally, returns the highest index i such that source.subList(i, i+target.size()).equals(target), or -1 if there is no such i ... more >
static ArrayList
list (Enumeration e)
Returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that require collections.
static Object
max (Collection coll)
Returns the maximum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements in the collection must be mutually comparable (that is, e1.compareTo(e2) must not t ... more >
static Object
max (Collection coll, Comparator comp)
Returns the maximum element of the given collection, according to the order induced by the specified comparator. All elements in the collection must be mutually comparable by the specified comparator (that is, comp.compare(e1, e2) must not throw a ClassCastException for any elements e ... more >
static Object
min (Collection coll)
Returns the minimum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements in the collection must be mutually comparable (that is, e1.compareTo(e2) must not t ... more >
static Object
min (Collection coll, Comparator comp)
Returns the minimum element of the given collection, according to the order induced by the specified comparator. All elements in the collection must be mutually comparable by the specified comparator (that is, comp.compare(e1, e2) must not throw a ClassCastException for any elements e ... more >
static List
nCopies (int n, Object o)
Returns an immutable list consisting of n copies of the specified object. The newly allocated data object is tiny (it contains a single reference to the data object). This method is useful in combination with the List.addAll method to grow lists. The returned list is serializable.
static boolean
replaceAll (List list, Object oldVal, Object newVal)
Replaces all occurrences of one specified value in a list with another. More formally, replaces with newVal each element e in list such that (oldVal==null ? e==null : oldVal.equals(e)). (This method has no effect on the size of the list.)
static void
reverse (List list)
Reverses the order of the elements in the specified list. This method runs in linear time.
static Comparator
reverseOrder ()
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) coll ... more >
static Comparator
reverseOrder (Comparator cmp)
Returns a comparator that imposes the reverse ordering of the specified comparator. If the specified comparator is null, this method is equivalent to #reverseOrder() (in other words, it returns a comparator that imposes the reverse of the natural ordering on a collection of objects th ... more >
static void
rotate (List list, int distance)
Rotates the elements in the specified list by the specified distance. After calling this method, the element at index i will be the element previously at index (i - distance) mod list.size(), for all values of i between 0 and list.size()-1, inclusive. (This method has no effect on th ... more >
static void
shuffle (List list)
Randomly permutes the specified list using a default source of randomness. All permutations occur with approximately equal likelihood. The hedge "approximately" is used in the foregoing description because default source of randomness is only approximately an unbiased source of ind ... more >
static void
shuffle (List list, Random rnd)
Randomly permute the specified list using the specified source of randomness. All permutations occur with equal likelihood assuming that the source of randomness is fair. This implementation traverses the list backwards, from the last element up to the second, repeatedly swapping a ... more >
static Set
singleton (Object o)
Returns an immutable set containing only the specified object. The returned set is serializable.
static List
singletonList (Object o)
Returns an immutable list containing only the specified object. The returned list is serializable.
static Map
singletonMap (Object key, Object value)
Returns an immutable map, mapping only the specified key to the specified value. The returned map is serializable.
static void
sort (List list)
Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastE ... more >
static void
sort (List list, Comparator c)
Sorts the specified list according to the order induced by the specified comparator. All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the list). This sor ... more >
static void
swap (List list, int i, int j)
Swaps the elements at the specified positions in the specified list. (If the specified positions are equal, invoking this method leaves the list unchanged.)
static Collection
synchronizedCollection (Collection c)
Returns a synchronized (thread-safe) collection backed by the specified collection. In order to guarantee serial access, it is critical that all access to the backing collection is accomplished through the returned collection. It is imperative that the user manually synchronize on t ... more >
static List
synchronizedList (List list)
Returns a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list. It is imperative that the user manually synchronize on the returned list when i ... more >
static Map
synchronizedMap (Map m)
Returns a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that all access to the backing map is accomplished through the returned map. It is imperative that the user manually synchronize on the returned map when iterat ... more >
static Set
synchronizedSet (Set s)
Returns a synchronized (thread-safe) set backed by the specified set. In order to guarantee serial access, it is critical that all access to the backing set is accomplished through the returned set. It is imperative that the user manually synchronize on the returned set when iterat ... more >
static SortedMap
synchronizedSortedMap (SortedMap m)
Returns a synchronized (thread-safe) sorted map backed by the specified sorted map. In order to guarantee serial access, it is critical that all access to the backing sorted map is accomplished through the returned sorted map (or its views). It is imperative that the user manually s ... more >
static SortedSet
synchronizedSortedSet (SortedSet s)
Returns a synchronized (thread-safe) sorted set backed by the specified sorted set. In order to guarantee serial access, it is critical that all access to the backing sorted set is accomplished through the returned sorted set (or its views). It is imperative that the user manually s ... more >
static Collection
unmodifiableCollection (Collection c)
Returns an unmodifiable view of the specified collection. This method allows modules to provide users with "read-only" access to internal collections. Query operations on the returned collection "read through" to the specified collection, and attempts to modify the returned collectio ... more >
static List
unmodifiableList (List list)
Returns an unmodifiable view of the specified list. This method allows modules to provide users with "read-only" access to internal lists. Query operations on the returned list "read through" to the specified list, and attempts to modify the returned list, whether direct or via its i ... more >
static Map
unmodifiableMap (Map m)
Returns an unmodifiable view of the specified map. This method allows modules to provide users with "read-only" access to internal maps. Query operations on the returned map "read through" to the specified map, and attempts to modify the returned map, whether direct or via its collec ... more >
static Set
unmodifiableSet (Set s)
Returns an unmodifiable view of the specified set. This method allows modules to provide users with "read-only" access to internal sets. Query operations on the returned set "read through" to the specified set, and attempts to modify the returned set, whether direct or via its iterato ... more >
static SortedMap
unmodifiableSortedMap (SortedMap m)
Returns an unmodifiable view of the specified sorted map. This method allows modules to provide users with "read-only" access to internal sorted maps. Query operations on the returned sorted map "read through" to the specified sorted map. Attempts to modify the returned sorted map, ... more >
static SortedSet
unmodifiableSortedSet (SortedSet s)
Returns an unmodifiable view of the specified sorted set. This method allows modules to provide users with "read-only" access to internal sorted sets. Query operations on the returned sorted set "read through" to the specified sorted set. Attempts to modify the returned sorted set, ... more >

Inherited methods

Community comments



Powered by JavaDocPlus