FileDocCategorySizeDatePackage
AbstractCollection.javaAPI DocAndroid 1.5 API16380Wed May 06 22:41:04 BST 2009java.util

AbstractCollection

public abstract class AbstractCollection extends Object implements Collection
Class {@code AbstractCollection} is an abstract implementation of the {@code Collection} interface. A subclass must implement the abstract methods {@code iterator()} and {@code size()} to create an immutable collection. To create a modifiable collection it's necessary to override the {@code add()} method that currently throws an {@code UnsupportedOperationException}.
since
Android 1.0

Fields Summary
Constructors Summary
protected AbstractCollection()
Constructs a new instance of this {@code AbstractCollection}.

since
Android 1.0

        super();
    
Methods Summary
public booleanadd(E object)

see
Collection#add

        throw new UnsupportedOperationException();
    
public booleanaddAll(java.util.Collection collection)
Attempts to add all of the objects contained in {@code collection} to the contents of this {@code Collection} (optional). This implementation iterates over the given {@code Collection} and calls {@code add} for each element. If any of these calls return {@code true}, then {@code true} is returned as result of this method call, {@code false} otherwise. If this {@code Collection} does not support adding elements, an {@code UnsupportedOperationException} is thrown.

If the passed {@code Collection} is changed during the process of adding elements to this {@code Collection}, the behavior depends on the behavior of the passed {@code Collection}.

param
collection the collection of objects.
return
{@code true} if this {@code Collection} is modified, {@code false} otherwise.
exception
UnsupportedOperationException when adding to this {@code Collection} is not supported.
exception
ClassCastException when the class of an object is inappropriate for this {@code Collection}.
exception
IllegalArgumentException when an object cannot be added to this {@code Collection}.
exception
NullPointerException when {@code collection} is {@code null}, or if it contains {@code null} elements and this {@code Collection} does not support such elements.
since
Android 1.0

        boolean result = false;
        Iterator<? extends E> it = collection.iterator();
        while (it.hasNext()) {
            if (add(it.next())) {
                result = true;
            }
        }
        return result;
    
public voidclear()
Removes all elements from this {@code Collection}, leaving it empty (optional). This implementation iterates over this {@code Collection} and calls the {@code remove} method on each element. If the iterator does not support removal of elements, an {@code UnsupportedOperationException} is thrown.

Concrete implementations usually can clear a {@code Collection} more efficiently and should therefore overwrite this method.

exception
UnsupportedOperationException when the iterator does not support removing elements from this {@code Collection}
see
#iterator
see
#isEmpty
see
#size
since
Android 1.0

        Iterator<E> it = iterator();
        while (it.hasNext()) {
            it.next();
            it.remove();
        }
    
public booleancontains(java.lang.Object object)
Tests whether this {@code Collection} contains the specified object. This implementation iterates over this {@code Collection} and tests, whether any element is equal to the given object. If {@code object != null} then {@code object.equals(e)} is called for each element {@code e} returned by the iterator until the element is found. If {@code object == null} then each element {@code e} returned by the iterator is compared with the test {@code e == null}.

param
object the object to search for.
return
{@code true} if object is an element of this {@code Collection}, {@code false} otherwise.
exception
ClassCastException if the object to look for isn't of the correct type.
exception
NullPointerException if the object to look for is {@code null} and this {@code Collection} doesn't support {@code null} elements.
since
Android 1.0

        Iterator<E> it = iterator();
        if (object != null) {
            while (it.hasNext()) {
                if (object.equals(it.next())) {
                    return true;
                }
            }
        } else {
            while (it.hasNext()) {
                if (it.next() == null) {
                    return true;
                }
            }
        }
        return false;
    
public booleancontainsAll(java.util.Collection collection)
Tests whether this {@code Collection} contains all objects contained in the specified {@code Collection}. This implementation iterates over the specified {@code Collection}. If one element returned by the iterator is not contained in this {@code Collection}, then {@code false} is returned; {@code true} otherwise.

param
collection the collection of objects.
return
{@code true} if all objects in the specified {@code Collection} are elements of this {@code Collection}, {@code false} otherwise.
exception
ClassCastException if one or more elements of {@code collection} isn't of the correct type.
exception
NullPointerException if {@code collection} contains at least one {@code null} element and this {@code Collection} doesn't support {@code null} elements.
exception
NullPointerException if {@code collection} is {@code null}.
since
Android 1.0

        Iterator<?> it = collection.iterator();
        while (it.hasNext()) {
            if (!contains(it.next())) {
                return false;
            }
        }
        return true;
    
public booleanisEmpty()
Returns if this {@code Collection} contains no elements. This implementation tests, whether {@code size} returns 0.

return
{@code true} if this {@code Collection} has no elements, {@code false} otherwise.
see
#size
since
Android 1.0

        return size() == 0;
    
public abstract java.util.Iteratoriterator()
Returns an instance of {@link Iterator} that may be used to access the objects contained by this {@code Collection}. The order in which the elements are returned by the {@link Iterator} is not defined unless the instance of the {@code Collection} has a defined order. In that case, the elements are returned in that order.

In this class this method is declared abstract and has to be implemented by concrete {@code Collection} implementations.

return
an iterator for accessing the {@code Collection} contents.
since
Android 1.0

public booleanremove(java.lang.Object object)
Removes one instance of the specified object from this {@code Collection} if one is contained (optional). This implementation iterates over this {@code Collection} and tests for each element {@code e} returned by the iterator, whether {@code e} is equal to the given object. If {@code object != null} then this test is performed using {@code object.equals(e)}, otherwise using {@code object == null}. If an element equal to the given object is found, then the {@code remove} method is called on the iterator and {@code true} is returned, {@code false} otherwise. If the iterator does not support removing elements, an {@code UnsupportedOperationException} is thrown.

param
object the object to remove.
return
{@code true} if this {@code Collection} is modified, {@code false} otherwise.
exception
UnsupportedOperationException when removing from this {@code Collection} is not supported.
exception
ClassCastException when the object passed is not of the correct type.
exception
NullPointerException if {@code object} is {@code null} and this {@code Collection} doesn't support {@code null} elements.
since
Android 1.0

        Iterator<?> it = iterator();
        if (object != null) {
            while (it.hasNext()) {
                if (object.equals(it.next())) {
                    it.remove();
                    return true;
                }
            }
        } else {
            while (it.hasNext()) {
                if (it.next() == null) {
                    it.remove();
                    return true;
                }
            }
        }
        return false;
    
public booleanremoveAll(java.util.Collection collection)
Removes all occurrences in this {@code Collection} of each object in the specified {@code Collection} (optional). After this method returns none of the elements in the passed {@code Collection} can be found in this {@code Collection} anymore.

This implementation iterates over this {@code Collection} and tests for each element {@code e} returned by the iterator, whether it is contained in the specified {@code Collection}. If this test is positive, then the {@code remove} method is called on the iterator. If the iterator does not support removing elements, an {@code UnsupportedOperationException} is thrown.

param
collection the collection of objects to remove.
return
{@code true} if this {@code Collection} is modified, {@code false} otherwise.
exception
UnsupportedOperationException when removing from this {@code Collection} is not supported.
exception
ClassCastException if one or more elements of {@code collection} isn't of the correct type.
exception
NullPointerException if {@code collection} contains at least one {@code null} element and this {@code Collection} doesn't support {@code null} elements.
exception
NullPointerException if {@code collection} is {@code null}.
since
Android 1.0

        boolean result = false;
        Iterator<?> it = iterator();
        while (it.hasNext()) {
            if (collection.contains(it.next())) {
                it.remove();
                result = true;
            }
        }
        return result;
    
public booleanretainAll(java.util.Collection collection)
Removes all objects from this {@code Collection} that are not also found in the {@code Collection} passed (optional). After this method returns this {@code Collection} will only contain elements that also can be found in the {@code Collection} passed to this method.

This implementation iterates over this {@code Collection} and tests for each element {@code e} returned by the iterator, whether it is contained in the specified {@code Collection}. If this test is negative, then the {@code remove} method is called on the iterator. If the iterator does not support removing elements, an {@code UnsupportedOperationException} is thrown.

param
collection the collection of objects to retain.
return
{@code true} if this {@code Collection} is modified, {@code false} otherwise.
exception
UnsupportedOperationException when removing from this {@code Collection} is not supported.
exception
ClassCastException if one or more elements of {@code collection} isn't of the correct type.
exception
NullPointerException if {@code collection} contains at least one {@code null} element and this {@code Collection} doesn't support {@code null} elements.
exception
NullPointerException if {@code collection} is {@code null}.
since
Android 1.0

        boolean result = false;
        Iterator<?> it = iterator();
        while (it.hasNext()) {
            if (!collection.contains(it.next())) {
                it.remove();
                result = true;
            }
        }
        return result;
    
public abstract intsize()
Returns a count of how many objects this {@code Collection} contains.

In this class this method is declared abstract and has to be implemented by concrete {@code Collection} implementations.

return
how many objects this {@code Collection} contains, or {@code Integer.MAX_VALUE} if there are more than {@code Integer.MAX_VALUE} elements in this {@code Collection}.
since
Android 1.0

public java.lang.Object[]toArray()

        int size = size(), index = 0;
        Iterator<?> it = iterator();
        Object[] array = new Object[size];
        while (index < size) {
            array[index++] = it.next();
        }
        return array;
    
public T[]toArray(T[] contents)

        int size = size(), index = 0;
        if (size > contents.length) {
            Class<?> ct = contents.getClass().getComponentType();
            contents = (T[])Array.newInstance(ct, size);
        }
        for (E entry: this) {
            contents[index++] = (T)entry;
        }
        if (index < contents.length) {
            contents[index] = null;
        }
        return contents;
    
public java.lang.StringtoString()
Returns the string representation of this {@code Collection}. The presentation has a specific format. It is enclosed by square brackets ("[]"). Elements are separated by ', ' (comma and space).

return
the string representation of this {@code Collection}.
since
Android 1.0

        if (isEmpty()) {
            return "[]"; //$NON-NLS-1$
        }

        StringBuilder buffer = new StringBuilder(size() * 16);
        buffer.append('[");
        Iterator<?> it = iterator();
        while (it.hasNext()) {
            Object next = it.next();
            if (next != this) {
                buffer.append(next);
            } else {
                buffer.append("(this Collection)"); //$NON-NLS-1$
            }
            if(it.hasNext()) {
                buffer.append(", "); //$NON-NLS-1$
            }
        }
        buffer.append(']");
        return buffer.toString();