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
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
Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods.
Capsule volume formula
The formula for capsule volume is as follows:
Capsule volume = Cylindrical volume Volume Two hemisphere volume
in,
r: The radius of the hemisphere.
h: The height of the cylinder (excluding the hemisphere).
Example 1
enter
Radius = 5 units
Height = 10 units
Output
Volume = 1570.8 cubic units
explain
Calculate volume using formula:
Volume = π × r2 × h (4
Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.