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!
Statement of this 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
PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation?
Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems.
Further reading: Java Stream API improvements
Understand Stream forEach
The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.
The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.