Overview of Java Collection interfaces

Source: Internet
Author: User
Tags hash iterable

The 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 implementation of this interface: it provides more specific sub-interfaces (such as Set and List) implementations. This interface is typically used to pass collection, and to manipulate these collection where the maximum universality is required.

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 via one of its sub-interfaces, should provide two "standard" constructs: one is a void (parameterless) construct method, which is used to create an empty Collection, and the other is a Collection A method of constructing a type single parameter that is used to create a new collection with the same elements as its arguments. 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 unsupportedoperationexception if 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 may not necessarily throw an exception when you invoke the AddAll (collection) 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 defined according to the Equals method. For example, the specification declaration of the CONTAINS (Object O) method returns True if 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 the Collection.contains method with Non-null parameter o causes the O.equals (e) method to be invoked for any e element. 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. (The Object.hashcode () specification guarantees that two objects that are not equal in hash codes are not equal). More commonly, implementations of various collections Framework interfaces are free to use the specified behavior of the underlying object method, regardless of whether the implementation program considers it appropriate.

The meaning of the iterator interface returned by the parent interface of the collection collection:

public interface iterator<e>

An iterator that iterates over the collection. The iterator replaces the enumeration in the Java collections Framework. Iterators differ from enumerations in two different ways: iterators allow callers to use well-defined semantics to remove elements from the collection that the iterator points to during the iteration. The method name has been improved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.