GeezerVison is showing docs.oracle.com — AbstractCollection (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.util
Class AbstractCollection<E>
-
-
Constructor Summary
Constructors
| Modifier |
Constructor |
Description |
protected |
AbstractCollection() |
Sole constructor.
|
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods
| Modifier and Type |
Method |
Description |
boolean |
add(E e) |
Ensures that this collection contains the specified element (optional
operation).
|
boolean |
addAll(Collection<? extends E> c) |
Adds all of the elements in the specified collection to this collection
(optional operation).
|
void |
clear() |
Removes all of the elements from this collection (optional operation).
|
boolean |
contains(Object o) |
Returns true if this collection contains the specified element.
|
boolean |
containsAll(Collection<?> c) |
Returns true if this collection contains all of the elements
in the specified collection.
|
boolean |
isEmpty() |
Returns true if this collection contains no elements.
|
abstract Iterator<E> |
iterator() |
Returns an iterator over the elements contained in this collection.
|
boolean |
remove(Object o) |
Removes a single instance of the specified element from this
collection, if it is present (optional operation).
|
boolean |
removeAll(Collection<?> c) |
Removes all of this collection's elements that are also contained in the
specified collection (optional operation).
|
boolean |
retainAll(Collection<?> c) |
Retains only the elements in this collection that are contained in the
specified collection (optional operation).
|
abstract int |
size() |
Returns the number of elements in this collection.
|
Object[] |
toArray() |
Returns an array containing all of the elements in this collection.
|
<T> T[] |
toArray(T[] a) |
Returns an array containing all of the elements in this collection;
the runtime type of the returned array is that of the specified array.
|
String |
toString() |
Returns a string representation of this collection.
|
-
-
Constructor Detail
-
AbstractCollection
protected AbstractCollection()
Sole constructor. (For invocation by subclass constructors, typically
implicit.)
-
Method Detail
-
size
public abstract int size()
Returns the number of elements in this collection. If this collection
contains more than Integer.MAX_VALUE elements, returns
Integer.MAX_VALUE.
- Specified by:
size in interface Collection<E>
- Returns:
- the number of elements in this collection
-
isEmpty
public boolean isEmpty()
Returns true if this collection contains no elements.
This implementation returns size() == 0.
- Specified by:
isEmpty in interface Collection<E>
- Returns:
- true if this collection contains no elements
-
add
public boolean add(E e)
Ensures that this collection contains the specified element (optional
operation). Returns true if this collection changed as a
result of the call. (Returns false if this collection does
not permit duplicates and already contains the specified element.)
Collections that support this operation may place limitations on what
elements may be added to this collection. In particular, some
collections will refuse to add null elements, and others will
impose restrictions on the type of elements that may be added.
Collection classes should clearly specify in their documentation any
restrictions on what elements may be added.
If a collection refuses to add a particular element for any reason
other than that it already contains the element, it must throw
an exception (rather than returning false). This preserves
the invariant that a collection always contains the specified element
after this call returns.
This implementation always throws an
UnsupportedOperationException.
- Specified by:
add in interface Collection<E>
- Parameters:
e - element whose presence in this collection is to be ensured
- Returns:
- true if this collection changed as a result of the
call
- Throws:
UnsupportedOperationException - if the add operation
is not supported by this collection
ClassCastException - if the class of the specified element
prevents it from being added to this collection
NullPointerException - if the specified element is null and this
collection does not permit null elements
IllegalArgumentException - if some property of the element
prevents it from being added to this collection
IllegalStateException - if the element cannot be added at this
time due to insertion restrictions
-
clear
public void clear()
Removes all of the elements from this collection (optional operation).
The collection will be empty after this method returns.
This implementation iterates over this collection, removing each
element using the Iterator.remove operation. Most
implementations will probably choose to override this method for
efficiency.
Note that this implementation will throw an
UnsupportedOperationException if the iterator returned by this
collection's iterator method does not implement the
remove method and this collection is non-empty.
- Specified by:
clear in interface Collection<E>
- Throws:
UnsupportedOperationException - if the clear operation
is not supported by this collection
-
toString
public String toString()
Returns a string representation of this collection. The string
representation consists of a list of the collection's elements in the
order they are returned by its iterator, enclosed in square brackets
( "[]"). Adjacent elements are separated by the characters
", " (comma and space). Elements are converted to strings as
by String.valueOf(Object).
- Overrides:
toString in class Object
- Returns:
- a string representation of this collection
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|