Java Collection interface Overview

Source: Internet
Author: User
Tags iterable
The set always requires iteration. We do need to check every element in the set, so the set interface inherits the iterable <t> interface without exception, and the unique method of this interface

Iterator <t> Iterator()
Returns an iterator that iterates on a group of T-type elements.

You can return an iteraor interface inherited by all the set interfaces, so that all subclasses of the set framework can iteratively display their elements!

For example, the collection Root Interface collection inherits the iterator interface:

Public interfaceCollection <E>
Extends iterable <E>
 
 

Collection hierarchy. Collection indicates a group of objects. These objects are also called collection objects.Element. Some collections allow repeated elements, while others do not. Some collections are ordered, while others are disordered. JDK does not provide anyDirectImplementation: it provides more specific sub-interfaces (suchSetAndList. This interface is usually used to pass collections and operate these collections where the maximum universality is required.

Bag)OrMulti-set (Multiset)(Unordered collection that may contain duplicate elements) This interface should be implemented directly.

All commonCollectionImplementation class (usually indirectly implemented through one of its subinterfacesCollection) Two "standard" constructor methods should be provided: one is void (No parameter) constructor, which is used to create an empty collection; the other isCollectionThe construction method of a single parameter of the type, used to create a collection with the same elements as its parameters. 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 the interface cannot contain constructor), all commonCollectionAll implementations follow it.

The "destructive" method contained in this interface refers to the methods that can be used to modify the collection objects to be operated by this interface. If this collection does not support this operation, specify these methods to throwUnsupportedoperationexception. If so, these methods are possible when the call is ineffective for the collection, but not necessarily throwUnsupportedoperationexception. For example, if the collection to be added is empty and cannot be modifiedAddall (Collection)Method, but not necessarily throw an exception.

Some collection implementations have restrictions on the elements they may contain. For example, some implementations prohibit null elements, while some implementations limit the type of elements. Attempting to add an unqualified element will throw an unchecked exception, usuallyNullpointerexceptionOrClasscastexception. An attempt to query whether an element is unqualified may throw an exception or simply return false. Some implementations will show the previous behavior, while some implementations will show the latter. It is common that an attempt to perform an operation on an unqualified element will not insert the unqualified element into the collection, and an exception may be thrown, the operation may also succeed, depending on the implementation itself. This exception is marked as "optional" in this interface specification ".

This interface is a member of Java collections framework.

Many methods in the collections framework interface are based onEqualsMethod definition. For example,Contains (Object O)Canonicalized statement of the method: "if and only when this collection contains at least one(O = NULL? E = NULL: O. Equals (e ))ElementEIs returned.True."NoThis specification should be understood to imply that a call has a non-null ParameterOOfCollection. ContainsThe method causes anyEElement callO. Equals (E)Method. You can optimize various implementations without callingEqualsYou can, for example, compare the hash codes of two elements. (Object. hashcode ()The two objects whose hash codes are not equal ). It is common that the implementation of various collections framework interfaces can be used freely.ObjectSpecifies the behavior of the method, regardless of the implementationProgramDetermines whether it is appropriate.

Meaning of the iterator interface returned by the parent interface of collection:

Public interfaceIterator <E>
 
 

Iterator used to iterate the set. The iterator replaces enumeration in Java collections framework. There are two differences between the iterator and enumeration:

    • The iterator allows the caller to remove elements from the set pointed to by the iterator during iteration using well-defined semantics.
    • 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.