The Collection interface is the root interface in the Collection hierarchy. Collection represents a set of objects, which are also called elements of the collection. Some collections (List,Queue) allow duplicate elements, while others (Set) do not. Some collections (List, Queue) are ordered, while others (Set) are unordered. The JDK does not provide any direct implementation of this interface: it provides implementations of more specific sub-interfaces such as Set and List, which inherit from the Collection interface. This interface is typically used to pass collections (polymorphically) and operate on them where maximum generality is required.
All general Collection implementation classes (usually implementing Collection indirectly through one of its sub-interfaces) should provide two "standard" constructor methods: One is a void (no parameter) constructor, used to create an empty collection; The other is a constructor with a single parameter of Collection type, used to create a new one with the same elements as its parameter. collection. In fact, the latter allows the user to copy any collection to generate an equivalent collection of the desired implementation type. Although this convention cannot be enforced (because interfaces cannot contain constructors), all common Collection implementations in the Java platform libraries adhere to it.
The "destructive" methods contained in this interface refer to those methods that can modify the collection they operate on. If this collection does not support the operation, these methods are specified to throw UnsupportedOperationException(optional operation).
Many methods in the Collections Framework interface are defined based on the equals method.
Each container in Collections Framework is non-threaded safe.
The definition of Collection interface is as follows:
public interface Collection<E> extends Iterable<E> { ... }
The java.util.Collection interface is the root interface that describes the Set and List collection types. All behaviors of Collection are given below (excluding methods inherited from Object) , so they also characterize all the behavior performed by a Set or List (List has additional functionality). The behavior classification and detailed list of Collection are as follows:
Table 1. Operations of Collection
Function
Introduction
Note
##boolean add(T)
Add the specified element to the collection. If the element is not added to the container, return false
Returns an Object type array containing all elements of the container
Array
Array The bridge between Collection and AbstractCollection provides a default implementation, and subclasses can override it
T[] toArray(T[] a)
return An Object array containing all elements of the container, and the runtime type of the returned result is the same as the parameter array a, rather than simply a bridge between object
Array and Collection. A default implementation is provided in AbstractCollection. Subclasses can override it
private class Itr implements Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0; /**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1; /**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount;
public boolean hasNext()
{
return cursor != size();
} public E next()
{
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
}
catch (IndexOutOfBoundsException e)
{
checkForComodification();
throw new NoSuchElementException();
}
} public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
//直接调用 AbstractList 容器的 remove(int index) 方法,抛出 UnsupportedOperationException
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
} final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
Copy after login
综上可知,示例中的 Arrays.asList() 部分为何会导致那样的结果。
The above is the detailed content of Java Collection Framework -Detailed explanation of Collection interface. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn