Beginners | collection | The interface collection always needs to be iterative, and we do need to explore every element in the collection, so the collection interface inherits the Iterable<t> interface without exception, and the interface's only method:
Iterator<T> |
iterator() Returns an iterator that iterates over an element of a set of T types. |
You can return a ITERAOR interface where all the collection interfaces inherit, so that all subclasses of the collection frame can iterate over their elements!
For example: The root interface of the collection collection inherits the iterator interface:
-
public interface
collection<e>
-
Extends iterable<e>
The root interface in the Collection hierarchy . Collection represents a set of objects, also called Collection elements . Some collection allow for duplicate elements, while others are not allowed. Some of the collection are ordered, while others are unordered. JDK does not provide any direct SetList for this interface
Packages (bag) or multiple collections (multiset)(unordered collection that may contain duplicate elements) should implement this interface directly.
All common Collection implementation classes (usually indirectly implemented through one of its sub-interfaces Collection) should provide two "standard" constructs: one is a void (parameterless) construction method, which is used to create an empty Collection Another is a constructor with the Collection type single parameter, used to create a new Collection with the same element as its argument. In fact, the latter allows the user to replicate any collection to produce an equivalent collection of the desired implementation type. Although this Convention cannot be enforced (because an interface cannot contain a construction method), all common Collection implementations in the Java Platform library comply with it.
The "destructive" method contained in this interface refers to those methods that can modify the collection that it operates, specifying that these methods throw a unsupportedoperationexceptionif the operation is not supported by this collection. If so, these methods may, but do not necessarily throw unsupportedoperationexception, when the call is invalid for the collection. For example, if the collection you want to add is empty and cannot be modified, you addAll(Collection)
may not necessarily throw an exception when you call the method on the collection.
Some collection implementations limit the elements they may contain. For example, some implementations prohibit null elements, while some implementations have restrictions on the type of elements. Attempting to add unqualified elements throws an unchecked exception, usually nullpointerexception or classcastexception. An attempt was made to query whether an unqualified element might throw an exception, or simply return false; some implementations would show the previous behavior, and some implementations would behave the latter. It is more common to attempt to perform an operation on an unqualified element and the completion of the operation does not result in the insertion of unqualified elements into collection, which may throw an exception, or the operation succeeds, depending on the implementation itself. Such exceptions are marked as "optional" in the specification of this interface.
This interface is a member of the Java collections Framework.
Many of the methods in the collections Framework interface are equals
defined by the method. For example, the contains(Object o)
canonical declaration of a method returns trueif and only if this collection contains at least one element e that satisfies (o==null. E==null:o.equals (e)) . "This specification should not be construed as implying that calling a Collection.contains method with a non-null argument o will result in the invocation of o.equals (e) for any e element method. You can perform optimizations on a variety of implementations at will, as long as you avoid calling equals , for example, by first comparing the hash codes of two elements. ( Object.hashCode()
the specification guarantees that two objects that are not equal to the hash code are not equal). More commonly, implementations of various collections Framework interfaces are free to take advantage Object
of the specified behavior of the underlying method, regardless of whether the implementation program deems it appropriate.
The meaning of the iterator interface returned by the parent interface of the collection collection:
-
Iterator<e>
An iterator that iterates over the collection. The iterator replaces the enumeration in the Java collections Framework. An iterator differs from an enumeration by two points:
- Iterators allow callers to take advantage of well-defined semantics to remove elements from the collection that the iterator points to during the iteration.
- The method name has been improved.