Beginner's note: Java Collection interface Overview

Source: Internet
Author: User
Tags iterable
Beginners must read: Java Collection interface overview-general Linux technology-Linux programming and kernel information. The following is a detailed description. The set always requires iteration. We do need to check every element in the set, so the set interface inherits the Iterable without exception. Interface, and the unique method of this interface:

Iterator Iterator ()

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 interface Collection
Extends Iterable
The Root Interface in the Collection hierarchy.
Collection represents a group of objects, also known as collection elements.
Some collections allow repeated elements, while others do not.
Some collections are ordered, while others are disordered.
JDK does not provide any direct

Implementation: it provides more specific sub-interfaces (such as Set and List) implementation.
This interface is usually used to pass collections and operate these collections where the maximum universality is required.
This interface should be directly implemented in a bag or multi-set (unordered collection that may contain duplicate elements.

All common Collection implementation classes (usually indirectly implement Collection through a sub-interface) should provide two "standard" constructor Methods: one is void (No parameter) constructor, creates an empty collection. The other is a constructor with a Collection type single parameter. It is 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 methods), all common Collection implementations in the Java platform library 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 on. If this collection does not support this operation, specify these methods to throw UnsupportedOperationException. If so, these methods are possible when the call is ineffective for the collection, but do not necessarily throw UnsupportedOperationException. For example, if the collection to be added is empty and cannot be modified, an exception may not be thrown when the addAll (collection) method is called for the Collection.

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, usually NullPointerException or ClassCastException. 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 defined according to the equals method. For example, the canonicalized declaration of the contains (Object o) method: "if and only when this collection contains at least one satisfied (o = null? E = null: true is returned only when the Element e of o. equals (e) is null ." This specification should not be understood as it implies that calling the Collection. contains method with non-null parameter o will cause the o. equals (e) method to be called for any e element. You can optimize various implementations at will, as long as you do not need to call equals, for example, by comparing the hash codes of the two elements first. (The Object. hashCode () specification ensures that two objects whose hash codes are not equal ). It is common that the implementation of various Collections Framework interfaces can randomly use the specified behavior of the basic Object method, regardless of whether the implementation program deems it appropriate.

Meaning of the Iterator interface returned by the parent interface of Collection:

Public interface Iterator
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.
Related Article

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.