FileDocCategorySizeDatePackage
Collections.javaAPI DocAndroid 1.5 API108897Wed May 06 22:41:04 BST 2009java.util

Collections

public class Collections extends Object
{@code Collections} contains static methods which operate on {@code Collection} classes.
since
Android 1.0

Fields Summary
public static final List
EMPTY_LIST
An empty immutable instance of {@link List}.
public static final Set
EMPTY_SET
An empty immutable instance of {@link Set}.
public static final Map
EMPTY_MAP
An empty immutable instance of {@link Map}.
Constructors Summary
private Collections()

        /* empty */
    
Methods Summary
public static booleanaddAll(java.util.Collection c, T a)
Adds all the specified elements to the specified collection.

param
c the collection the elements are to be inserted into.
param
a the elements to insert.
return
true if the collection changed during insertion.
throws
UnsupportedOperationException when the method is not supported.
throws
NullPointerException when {@code c} or {@code a} is {@code null}, or {@code a} contains one or more {@code null} elements and {@code c} doesn't support {@code null} elements.
throws
IllegalArgumentException if at least one of the elements can't be inserted into the collection.
since
Android 1.0

        boolean modified = false;
        for (int i = 0; i < a.length; i++) {
            modified |= c.add(a[i]);
        }
        return modified;
    
public static intbinarySearch(java.util.List list, T object)
Performs a binary search for the specified element in the specified sorted List. The List needs to be already sorted in natural sorting order. Searching in an unsorted array has an undefined result. It's also undefined which element is found if there are multiple occurrences of the same element.

param
list the sorted List to search.
param
object the element to find.
return
the non-negative index of the element, or a negative index which is the {@code -index - 1} where the element would be inserted
throws
ClassCastException if an element in the List or the search element does not implement Comparable, or cannot be compared to each other.
since
Android 1.0

        if (list == null) {
            throw new NullPointerException();
        }
        if (list.isEmpty()) {
            return -1;
        }

        Comparable<T> key = (Comparable<T>) object;
        if (!(list instanceof RandomAccess)) {
            ListIterator<T> it = (ListIterator<T>) list.listIterator();
            while (it.hasNext()) {
                int result;
                if ((result = key.compareTo(it.next())) <= 0) {
                    if (result == 0) {
                        return it.previousIndex();
                    }
                    return -it.previousIndex() - 1;
                }
            }
            return -list.size() - 1;
        }

        int low = 0, mid = list.size(), high = mid - 1, result = -1;
        while (low <= high) {
            mid = (low + high) >> 1;
            if ((result = key.compareTo((T) list.get(mid))) > 0) {
                low = mid + 1;
            } else if (result == 0) {
                return mid;
            } else {
                high = mid - 1;
            }
        }
        return -mid - (result < 0 ? 1 : 2);
    
public static intbinarySearch(java.util.List list, T object, java.util.Comparator comparator)
Performs a binary search for the specified element in the specified sorted List using the specified Comparator. The List needs to be already sorted according to the comparator passed. Searching in an unsorted array has an undefined result. It's also undefined which element is found if there are multiple occurrences of the same element.

param
list the sorted List to search.
param
object the element to find.
param
comparator the Comparator. If the comparator is {@code null} then the search uses the objects' natural ordering.
return
the non-negative index of the element, or a negative index which is the {@code -index - 1} where the element would be inserted.
throws
ClassCastException when an element in the list and the searched element cannot be compared to each other using the Comparator.
since
Android 1.0

        if (comparator == null) {
            return Collections.binarySearch(
                    (List<? extends Comparable<? super T>>) list, object);
        }
        if (!(list instanceof RandomAccess)) {
            ListIterator<? extends T> it = list.listIterator();
            while (it.hasNext()) {
                int result;
                if ((result = comparator.compare(object, it.next())) <= 0) {
                    if (result == 0) {
                        return it.previousIndex();
                    }
                    return -it.previousIndex() - 1;
                }
            }
            return -list.size() - 1;
        }

        int low = 0, mid = list.size(), high = mid - 1, result = -1;
        while (low <= high) {
            mid = (low + high) >> 1;
            if ((result = comparator.compare(object, list.get(mid))) > 0) {
                low = mid + 1;
            } else if (result == 0) {
                return mid;
            } else {
                high = mid - 1;
            }
        }
        return -mid - (result < 0 ? 1 : 2);
    
static EcheckType(E obj, java.lang.Class type)
Checks if specified object is instance of specified class. Used for a dynamically typesafe view of the collections.

param
obj - object is to be checked
param
type - class of object that should be
return
specified object

        if (!type.isInstance(obj)) {
            // luni.05=Attempt to insert {0} element into collection with
            // element type {1}
            throw new ClassCastException(Messages.getString(
                    "luni.05", obj.getClass(), type)); //$NON-NLS-1$
        }
        return obj;
    
public static java.util.CollectioncheckedCollection(java.util.Collection c, java.lang.Class type)
Returns a dynamically typesafe view of the specified collection. Trying to insert an element of the wrong type into this collection throws a {@code ClassCastException}. At creation time the types in {@code c} are not checked for correct type.

param
c the collection to be wrapped in a typesafe collection.
param
type the type of the elements permitted to insert.
return
a typesafe collection.
since
Android 1.0

        return new CheckedCollection<E>(c, type);
    
public static java.util.ListcheckedList(java.util.List list, java.lang.Class type)
Returns a dynamically typesafe view of the specified list. Trying to insert an element of the wrong type into this list throws a {@code ClassCastException}. At creation time the types in {@code list} are not checked for correct type.

param
list the list to be wrapped in a typesafe list.
param
type the type of the elements permitted to insert.
return
a typesafe list.
since
Android 1.0

        if (list instanceof RandomAccess) {
            return new CheckedRandomAccessList<E>(list, type);
        }
        return new CheckedList<E>(list, type);
    
public static java.util.MapcheckedMap(java.util.Map m, java.lang.Class keyType, java.lang.Class valueType)
Returns a dynamically typesafe view of the specified map. Trying to insert an element of the wrong type into this map throws a {@code ClassCastException}. At creation time the types in {@code m} are not checked for correct type.

param
m the map to be wrapped in a typesafe map.
param
keyType the type of the keys permitted to insert.
param
valueType the type of the values permitted to insert.
return
a typesafe map.
since
Android 1.0

        return new CheckedMap<K, V>(m, keyType, valueType);
    
public static java.util.SetcheckedSet(java.util.Set s, java.lang.Class type)
Returns a dynamically typesafe view of the specified set. Trying to insert an element of the wrong type into this set throws a {@code ClassCastException}. At creation time the types in {@code s} are not checked for correct type.

param
s the set to be wrapped in a typesafe set.
param
type the type of the elements permitted to insert.
return
a typesafe set.
since
Android 1.0

        return new CheckedSet<E>(s, type);
    
public static java.util.SortedMapcheckedSortedMap(java.util.SortedMap m, java.lang.Class keyType, java.lang.Class valueType)
Returns a dynamically typesafe view of the specified sorted map. Trying to insert an element of the wrong type into this sorted map throws a {@code ClassCastException}. At creation time the types in {@code m} are not checked for correct type.

param
m the sorted map to be wrapped in a typesafe sorted map.
param
keyType the type of the keys permitted to insert.
param
valueType the type of the values permitted to insert.
return
a typesafe sorted map.
since
Android 1.0

        return new CheckedSortedMap<K, V>(m, keyType, valueType);
    
public static java.util.SortedSetcheckedSortedSet(java.util.SortedSet s, java.lang.Class type)
Returns a dynamically typesafe view of the specified sorted set. Trying to insert an element of the wrong type into this sorted set throws a {@code ClassCastException}. At creation time the types in {@code s} are not checked for correct type.

param
s the sorted set to be wrapped in a typesafe sorted set.
param
type the type of the elements permitted to insert.
return
a typesafe sorted set.
since
Android 1.0

        return new CheckedSortedSet<E>(s, type);
    
public static voidcopy(java.util.List destination, java.util.List source)
Copies the elements from the source list to the destination list. At the end both lists will have the same objects at the same index. If the destination array is larger than the source list, the elements in the destination list with {@code index >= source.size()} will be unchanged.

param
destination the list whose elements are set from the source list.
param
source the list with the elements to be copied into the destination.
throws
IndexOutOfBoundsException when the destination List is smaller than the source List.
throws
UnsupportedOperationException when replacing an element in the destination list is not supported.
since
Android 1.0

        if (destination.size() < source.size()) {
            throw new ArrayIndexOutOfBoundsException();
        }
        Iterator<? extends T> srcIt = source.iterator();
        ListIterator<? super T> destIt = destination.listIterator();
        while (srcIt.hasNext()) {
            try {
                destIt.next();
            } catch (NoSuchElementException e) {
                throw new ArrayIndexOutOfBoundsException();
            }
            destIt.set(srcIt.next());
        }
    
public static booleandisjoint(java.util.Collection c1, java.util.Collection c2)
Returns whether the specified collections have no elements in common.

param
c1 the first collection.
param
c2 the second collection.
return
{@code true} if the collections have no elements in common, {@code false} otherwise.
throws
NullPointerException if one of the collections is {@code null}.
since
Android 1.0

        if ((c1 instanceof Set) && !(c2 instanceof Set)
                || (c2.size()) > c1.size()) {
            Collection<?> tmp = c1;
            c1 = c2;
            c2 = tmp;
        }
        Iterator<?> it = c1.iterator();
        while (it.hasNext()) {
            if (c2.contains(it.next())) {
                return false;
            }
        }
        return true;
    
public static final java.util.ListemptyList()
Returns a type-safe empty, immutable {@link List}.

return
an empty {@link List}.
see
#EMPTY_LIST
since
Android 1.0

        return EMPTY_LIST;
    
public static final java.util.MapemptyMap()
Returns a type-safe empty, immutable {@link Map}.

return
an empty {@link Map}.
see
#EMPTY_MAP
since
Android 1.0

        return EMPTY_MAP;
    
public static final java.util.SetemptySet()
Returns a type-safe empty, immutable {@link Set}.

return
an empty {@link Set}.
see
#EMPTY_SET
since
Android 1.0

        return EMPTY_SET;
    
public static java.util.Enumerationenumeration(java.util.Collection collection)
Returns an {@code Enumeration} on the specified collection.

param
collection the collection to enumerate.
return
an Enumeration.
since
Android 1.0

        final Collection<T> c = collection;
        return new Enumeration<T>() {
            Iterator<T> it = c.iterator();

            public boolean hasMoreElements() {
                return it.hasNext();
            }

            public T nextElement() {
                return it.next();
            }
        };
    
public static voidfill(java.util.List list, T object)
Fills the specified List with the specified element.

param
list the List to fill.
param
object the element to fill the list with.
throws
UnsupportedOperationException when replacing an element in the List is not supported.
since
Android 1.0

        ListIterator<? super T> it = list.listIterator();
        while (it.hasNext()) {
            it.next();
            it.set(object);
        }
    
public static intfrequency(java.util.Collection c, java.lang.Object o)
Returns the number of elements in the {@code Collection} that match the {@code Object} passed. If the {@code Object} is {@code null}, then the number of {@code null} elements is returned.

param
c the {@code Collection} to search.
param
o the {@code Object} to search for.
return
the number of matching elements.
throws
NullPointerException if the {@code Collection} parameter is {@code null}.
since
Android 1.0

        if (c == null) {
            throw new NullPointerException();
        }
        if (c.isEmpty()) {
            return 0;
        }
        int result = 0;
        Iterator<?> itr = c.iterator();
        while (itr.hasNext()) {
            Object e = itr.next();
            if (o == null ? e == null : o.equals(e)) {
                result++;
            }
        }
        return result;
    
public static intindexOfSubList(java.util.List list, java.util.List sublist)
Searches the {@code list} for {@code sublist} and returns the beginning index of the first occurrence.

-1 is returned if the {@code sublist} does not exist in {@code list}.

param
list the List to search {@code sublist} in.
param
sublist the List to search in {@code list}.
return
the beginning index of the first occurrence of {@code sublist} in {@code list}, or -1.
since
Android 1.0

        int size = list.size();
        int sublistSize = sublist.size();

        if (sublistSize > size) {
            return -1;
        }

        if (sublistSize == 0) {
            return 0;
        }

        // find the first element of sublist in the list to get a head start
        Object firstObj = sublist.get(0);
        int index = list.indexOf(firstObj);
        if (index == -1) {
            return -1;
        }

        while (index < size && (size - index >= sublistSize)) {
            ListIterator<?> listIt = list.listIterator(index);

            if ((firstObj == null) ? listIt.next() == null : firstObj
                    .equals(listIt.next())) {

                // iterate through the elements in sublist to see
                // if they are included in the same order in the list
                ListIterator<?> sublistIt = sublist.listIterator(1);
                boolean difFound = false;
                while (sublistIt.hasNext()) {
                    Object element = sublistIt.next();
                    if (!listIt.hasNext()) {
                        return -1;
                    }
                    if ((element == null) ? listIt.next() != null : !element
                            .equals(listIt.next())) {
                        difFound = true;
                        break;
                    }
                }
                // All elements of sublist are found in main list
                // starting from index.
                if (!difFound) {
                    return index;
                }
            }
            // This was not the sequence we were looking for,
            // continue search for the firstObj in main list
            // at the position after index.
            index++;
        }
        return -1;
    
public static intlastIndexOfSubList(java.util.List list, java.util.List sublist)
Searches the {@code list} for {@code sublist} and returns the beginning index of the last occurrence.

-1 is returned if the {@code sublist} does not exist in {@code list}.

param
list the List to search {@code sublist} in.
param
sublist the List to search in {@code list}.
return
the beginning index of the last occurrence of {@code sublist} in {@code list}, or -1.
since
Android 1.0

        int sublistSize = sublist.size();
        int size = list.size();

        if (sublistSize > size) {
            return -1;
        }

        if (sublistSize == 0) {
            return size;
        }

        // find the last element of sublist in the list to get a head start
        Object lastObj = sublist.get(sublistSize - 1);
        int index = list.lastIndexOf(lastObj);

        while ((index > -1) && (index + 1 >= sublistSize)) {
            ListIterator<?> listIt = list.listIterator(index + 1);

            if ((lastObj == null) ? listIt.previous() == null : lastObj
                    .equals(listIt.previous())) {
                // iterate through the elements in sublist to see
                // if they are included in the same order in the list
                ListIterator<?> sublistIt = sublist
                        .listIterator(sublistSize - 1);
                boolean difFound = false;
                while (sublistIt.hasPrevious()) {
                    Object element = sublistIt.previous();
                    if (!listIt.hasPrevious()) {
                        return -1;
                    }
                    if ((element == null) ? listIt.previous() != null
                            : !element.equals(listIt.previous())) {
                        difFound = true;
                        break;
                    }
                }
                // All elements of sublist are found in main list
                // starting from listIt.nextIndex().
                if (!difFound) {
                    return listIt.nextIndex();
                }
            }
            // This was not the sequence we were looking for,
            // continue search for the lastObj in main list
            // at the position before index.
            index--;
        }
        return -1;
    
public static java.util.ArrayListlist(java.util.Enumeration enumeration)
Returns an {@code ArrayList} with all the elements in the {@code enumeration}. The elements in the returned ArrayList are in the same order as in the {@code enumeration}.

param
enumeration the source {@link Enumeration}.
return
an {@code ArrayList} from {@code enumeration}.
since
Android 1.0

        ArrayList<T> list = new ArrayList<T>();
        while (enumeration.hasMoreElements()) {
            list.add(enumeration.nextElement());
        }
        return list;
    
public static Tmax(java.util.Collection collection)
Searches the specified Collection for the maximum element.

param
collection the Collection to search.
return
the maximum element in the Collection.
throws
ClassCastException when an element in the Collection does not implement {@code Comparable} or elements cannot be compared to each other.
since
Android 1.0

        Iterator<? extends T> it = collection.iterator();
        T max = it.next();
        while (it.hasNext()) {
            T next = it.next();
            if (max.compareTo(next) < 0) {
                max = next;
            }
        }
        return max;
    
public static Tmax(java.util.Collection collection, java.util.Comparator comparator)
Searches the specified Collection for the maximum element using the specified Comparator.

param
collection the Collection to search.
param
comparator the Comparator.
return
the maximum element in the Collection.
throws
ClassCastException when elements in the Collection cannot be compared to each other using the {@code Comparator}.
since
Android 1.0

        Iterator<? extends T> it = collection.iterator();
        T max = it.next();
        while (it.hasNext()) {
            T next = it.next();
            if (comparator.compare(max, next) < 0) {
                max = next;
            }
        }
        return max;
    
public static Tmin(java.util.Collection collection, java.util.Comparator comparator)
Searches the specified Collection for the minimum element using the specified Comparator.

param
collection the Collection to search.
param
comparator the Comparator.
return
the minimum element in the Collection.
throws
ClassCastException when elements in the Collection cannot be compared to each other using the {@code Comparator}.
since
Android 1.0

        Iterator<? extends T> it = collection.iterator();
        T min = it.next();
        while (it.hasNext()) {
            T next = it.next();
            if (comparator.compare(min, next) > 0) {
                min = next;
            }
        }
        return min;
    
public static Tmin(java.util.Collection collection)
Searches the specified Collection for the minimum element.

param
collection the Collection to search.
return
the minimum element in the Collection.
throws
ClassCastException when an element in the Collection does not implement {@code Comparable} or elements cannot be compared to each other.
since
Android 1.0

        Iterator<? extends T> it = collection.iterator();
        T min = it.next();
        while (it.hasNext()) {
            T next = it.next();
            if (min.compareTo(next) > 0) {
                min = next;
            }
        }
        return min;
    
public static java.util.ListnCopies(int length, T object)
Returns a List containing the specified number of the specified element. The list cannot be modified. The list is serializable.

param
length the size of the returned list.
param
object the element to be added {@code length} times to a list.
return
a List containing {@code length} copies of the element.
throws
IllegalArgumentException when {@code length < 0}.
since
Android 1.0

        return new CopiesList<T>(length, object);
    
public static booleanreplaceAll(java.util.List list, T obj, T obj2)
Replaces all occurrences of Object {@code obj} in {@code list} with {@code newObj}. If the {@code obj} is {@code null}, then all occurrences of {@code null} are replaced with {@code newObj}.

param
list the List to modify.
param
obj the Object to find and replace occurrences of.
param
obj2 the Object to replace all occurrences of {@code obj} in {@code list}.
return
true, if at least one occurrence of {@code obj} has been found in {@code list}.
throws
UnsupportedOperationException if the list does not support setting elements.
since
Android 1.0

        int index;
        boolean found = false;

        while ((index = list.indexOf(obj)) > -1) {
            found = true;
            list.set(index, obj2);
        }
        return found;
    
public static voidreverse(java.util.List list)
Modifies the specified {@code List} by reversing the order of the elements.

param
list the List to reverse.
throws
UnsupportedOperationException when replacing an element in the List is not supported.
since
Android 1.0

        int size = list.size();
        ListIterator<Object> front = (ListIterator<Object>) list.listIterator();
        ListIterator<Object> back = (ListIterator<Object>) list
                .listIterator(size);
        for (int i = 0; i < size / 2; i++) {
            Object frontNext = front.next();
            Object backPrev = back.previous();
            front.set(backPrev);
            back.set(frontNext);
        }
    
public static java.util.ComparatorreverseOrder()
A Comparator which reverses the natural order of the elements. The {@code Comparator} that's returned is {@link Serializable}.

return
a {@code Comparator} instance.
see
Comparator
see
Comparable
see
Serializable
since
Android 1.0

        return new ReverseComparator<T>();
    
public static java.util.ComparatorreverseOrder(java.util.Comparator c)
Returns a {@link Comparator} that reverses the order of the {@code Comparator} passed. If the {@code Comparator} passed is {@code null}, then this method is equivalent to {@link #reverseOrder()}.

The {@code Comparator} that's returned is {@link Serializable} if the {@code Comparator} passed is serializable or {@code null}.

param
c the {@code Comparator} to reverse or {@code null}.
return
a {@code Comparator} instance.
see
Comparator
since
Android 1.0

        if (c == null) {
            return reverseOrder();
        }
        return new ReverseComparatorWithComparator<T>(c);
    
public static voidrotate(java.util.List lst, int dist)
Rotates the elements in List {@code list} by the distance {@code dist}

e.g. for a given list with elements [1, 2, 3, 4, 5, 6, 7, 8, 9, 0], calling rotate(list, 3) or rotate(list, -7) would modify the list to look like this: [8, 9, 0, 1, 2, 3, 4, 5, 6, 7]

param
lst the list whose elements are to be rotated.
param
dist is the distance the list is rotated. This can be any valid integer. Negative values rotate the list backwards.
since
Android 1.0

        List<Object> list = (List<Object>) lst;
        int size = list.size();

        // Can't sensibly rotate an empty collection
        if (size == 0) {
            return;
        }

        // normalize the distance
        int normdist;
        if (dist > 0) {
            normdist = dist % size;
        } else {
            normdist = size - ((dist % size) * (-1));
        }

        if (normdist == 0 || normdist == size) {
            return;
        }

        if (list instanceof RandomAccess) {
            // make sure each element gets juggled
            // with the element in the position it is supposed to go to
            Object temp = list.get(0);
            int index = 0, beginIndex = 0;
            for (int i = 0; i < size; i++) {
                index = (index + normdist) % size;
                temp = list.set(index, temp);
                if (index == beginIndex) {
                    index = ++beginIndex;
                    temp = list.get(beginIndex);
                }
            }
        } else {
            int divideIndex = (size - normdist) % size;
            List<Object> sublist1 = list.subList(0, divideIndex);
            List<Object> sublist2 = list.subList(divideIndex, size);
            reverse(sublist1);
            reverse(sublist2);
            reverse(list);
        }
    
public static voidshuffle(java.util.List list)
Moves every element of the List to a random new position in the list.

param
list the List to shuffle.
throws
UnsupportedOperationException when replacing an element in the List is not supported.
since
Android 1.0

        shuffle(list, new Random());
    
public static voidshuffle(java.util.List list, java.util.Random random)
Moves every element of the List to a random new position in the list using the specified random number generator.

param
list the List to shuffle.
param
random the random number generator.
throws
UnsupportedOperationException when replacing an element in the List is not supported.
since
Android 1.0

        if (!(list instanceof RandomAccess)) {
            Object[] array = list.toArray();
            for (int i = array.length - 1; i > 0; i--) {
                int index = random.nextInt() % (i + 1);
                if (index < 0) {
                    index = -index;
                }
                Object temp = array[i];
                array[i] = array[index];
                array[index] = temp;
            }

            int i = 0;
            ListIterator<Object> it = (ListIterator<Object>) list
                    .listIterator();
            while (it.hasNext()) {
                it.next();
                it.set(array[i++]);
            }
        } else {
            List<Object> rawList = (List<Object>) list;
            for (int i = rawList.size() - 1; i > 0; i--) {
                int index = random.nextInt() % (i + 1);
                if (index < 0) {
                    index = -index;
                }
                rawList.set(index, rawList.set(i, rawList.get(index)));
            }
        }
    
public static java.util.Setsingleton(E object)
Returns a Set containing the specified element. The set cannot be modified. The set is serializable.

param
object the element.
return
a Set containing the element.
since
Android 1.0

        return new SingletonSet<E>(object);
    
public static java.util.ListsingletonList(E object)
Returns a List containing the specified element. The list cannot be modified. The list is serializable.

param
object the element.
return
a List containing the element.
since
Android 1.0

        return new SingletonList<E>(object);
    
public static java.util.MapsingletonMap(K key, V value)
Returns a Map containing the specified key and value. The map cannot be modified. The map is serializable.

param
key the key.
param
value the value.
return
a Map containing the key and value.
since
Android 1.0

        return new SingletonMap<K, V>(key, value);
    
public static voidsort(java.util.List list)
Sorts the specified List in ascending natural order. The algorithm is stable which means equal elements don't get reordered.

param
list the List to be sorted.
throws
ClassCastException when an element in the List does not implement Comparable or elements cannot be compared to each other.
since
Android 1.0

        Object[] array = list.toArray();
        Arrays.sort(array);
        int i = 0;
        ListIterator<T> it = list.listIterator();
        while (it.hasNext()) {
            it.next();
            it.set((T) array[i++]);
        }
    
public static voidsort(java.util.List list, java.util.Comparator comparator)
Sorts the specified List using the specified Comparator. The algorithm is stable which means equal elements don't get reordered.

param
list the List to be sorted.
param
comparator the Comparator.
throws
ClassCastException when elements in the List cannot be compared to each other using the Comparator.
since
Android 1.0

        T[] array = list.toArray((T[]) new Object[list.size()]);
        Arrays.sort(array, comparator);
        int i = 0;
        ListIterator<T> it = list.listIterator();
        while (it.hasNext()) {
            it.next();
            it.set(array[i++]);
        }
    
public static voidswap(java.util.List list, int index1, int index2)
Swaps the elements of List {@code list} at indices {@code index1} and {@code index2}.

param
list the List to manipulate.
param
index1 position of the first element to swap with the element in index2.
param
index2 position of the other element.
throws
IndexOutOfBoundsException if index1 or index2 is out of range of this list.
since
Android 1.0

        if (list == null) {
            throw new NullPointerException();
        }
        if (index1 == index2) {
            return;
        }
        List<Object> rawList = (List<Object>) list;
        rawList.set(index2, rawList.set(index1, rawList.get(index2)));
    
public static java.util.CollectionsynchronizedCollection(java.util.Collection collection)
Returns a wrapper on the specified Collection which synchronizes all access to the Collection.

param
collection the Collection to wrap in a synchronized collection.
return
a synchronized Collection.
since
Android 1.0

        if (collection == null) {
            throw new NullPointerException();
        }
        return new SynchronizedCollection<T>(collection);
    
public static java.util.ListsynchronizedList(java.util.List list)
Returns a wrapper on the specified List which synchronizes all access to the List.

param
list the List to wrap in a synchronized list.
return
a synchronized List.
since
Android 1.0

        if (list == null) {
            throw new NullPointerException();
        }
        if (list instanceof RandomAccess) {
            return new SynchronizedRandomAccessList<T>(list);
        }
        return new SynchronizedList<T>(list);
    
public static java.util.MapsynchronizedMap(java.util.Map map)
Returns a wrapper on the specified Map which synchronizes all access to the Map.

param
map the Map to wrap in a synchronized map.
return
a synchronized Map.
since
Android 1.0

        if (map == null) {
            throw new NullPointerException();
        }
        return new SynchronizedMap<K, V>(map);
    
public static java.util.SetsynchronizedSet(java.util.Set set)
Returns a wrapper on the specified Set which synchronizes all access to the Set.

param
set the Set to wrap in a synchronized set.
return
a synchronized Set.
since
Android 1.0

        if (set == null) {
            throw new NullPointerException();
        }
        return new SynchronizedSet<E>(set);
    
public static java.util.SortedMapsynchronizedSortedMap(java.util.SortedMap map)
Returns a wrapper on the specified SortedMap which synchronizes all access to the SortedMap.

param
map the SortedMap to wrap in a synchronized sorted map.
return
a synchronized SortedMap.
since
Android 1.0

        if (map == null) {
            throw new NullPointerException();
        }
        return new SynchronizedSortedMap<K, V>(map);
    
public static java.util.SortedSetsynchronizedSortedSet(java.util.SortedSet set)
Returns a wrapper on the specified SortedSet which synchronizes all access to the SortedSet.

param
set the SortedSet to wrap in a synchronized sorted set.
return
a synchronized SortedSet.
since
Android 1.0

        if (set == null) {
            throw new NullPointerException();
        }
        return new SynchronizedSortedSet<E>(set);
    
public static java.util.CollectionunmodifiableCollection(java.util.Collection collection)
Returns a wrapper on the specified Collection which throws an {@code UnsupportedOperationException} whenever an attempt is made to modify the Collection.

param
collection the Collection to wrap in an unmodifiable collection.
return
an unmodifiable Collection.
since
Android 1.0

        if (collection == null) {
            throw new NullPointerException();
        }
        return new UnmodifiableCollection<E>((Collection<E>) collection);
    
public static java.util.ListunmodifiableList(java.util.List list)
Returns a wrapper on the specified List which throws an {@code UnsupportedOperationException} whenever an attempt is made to modify the List.

param
list the List to wrap in an unmodifiable list.
return
an unmodifiable List.
since
Android 1.0

        if (list == null) {
            throw new NullPointerException();
        }
        if (list instanceof RandomAccess) {
            return new UnmodifiableRandomAccessList<E>((List<E>) list);
        }
        return new UnmodifiableList<E>((List<E>) list);
    
public static java.util.MapunmodifiableMap(java.util.Map map)
Returns a wrapper on the specified Map which throws an {@code UnsupportedOperationException} whenever an attempt is made to modify the Map.

param
map the Map to wrap in an unmodifiable map.
return
a unmodifiable Map.
since
Android 1.0

        if (map == null) {
            throw new NullPointerException();
        }
        return new UnmodifiableMap<K, V>((Map<K, V>) map);
    
public static java.util.SetunmodifiableSet(java.util.Set set)
Returns a wrapper on the specified Set which throws an {@code UnsupportedOperationException} whenever an attempt is made to modify the Set.

param
set the Set to wrap in an unmodifiable set.
return
a unmodifiable Set.
since
Android 1.0

        if (set == null) {
            throw new NullPointerException();
        }
        return new UnmodifiableSet<E>((Set<E>) set);
    
public static java.util.SortedMapunmodifiableSortedMap(java.util.SortedMap map)
Returns a wrapper on the specified SortedMap which throws an {@code UnsupportedOperationException} whenever an attempt is made to modify the SortedMap.

param
map the SortedMap to wrap in an unmodifiable sorted map.
return
a unmodifiable SortedMap.
since
Android 1.0

        if (map == null) {
            throw new NullPointerException();
        }
        return new UnmodifiableSortedMap<K, V>((SortedMap<K, V>) map);
    
public static java.util.SortedSetunmodifiableSortedSet(java.util.SortedSet set)
Returns a wrapper on the specified SortedSet which throws an {@code UnsupportedOperationException} whenever an attempt is made to modify the SortedSet.

param
set the SortedSet to wrap in an unmodifiable sorted set.
return
a unmodifiable SortedSet.
since
Android 1.0

        if (set == null) {
            throw new NullPointerException();
        }
        return new UnmodifiableSortedSet<E>(set);