A collection framework for the Java Foundation

Source: Internet
Author: User

Collection Framework (Collections framework)

First, make it clear that the collection represents a set of objects (and arrays, but the array length cannot change, and the collection Can). The collection framework in Java defines a set of specifications for representing, manipulating, and decoupling specific operations from implementation details.

In fact, you can think of a set as a micro-database, operation is nothing more than "delete and change" four kinds of operations, we learn to use a specific set of classes, we need to make these four operations 时空复杂度 clear, basically can say Master this class.

Design concept

The main idea in a nutshell is: To provide a set of "small and beautiful" API. The API needs to be programmer-friendly and add new functionality to enable programmers to get started quickly.
To ensure that the core interface is small enough, the topmost interface (that is, the collection and map interfaces) does not differentiate whether the set is mutable (mutability) and can be changed (modifiability). Whether it is possible to change the size (resizability) of these subtle differences. Conversely, some operations are optional and can be thrown when implemented to UnsupportedOperationException indicate that the collection does not support the operation. The implementation of the collection must declare in the document that those operations are not supported.

To ensure that the top-most core interfaces are small enough, they can only contain methods in the following situations:

    1. Basic operation, such as said before the "Delete and change"

    2. There is a compelling performance reason what an important implementation would want to override it.

In addition, all collection classes must be able to provide friendly interaction, including array objects without inheriting Collection classes. Therefore, the framework provides a set of methods that allow the collection classes and arrays to be converted to each other and can be Map considered as collections.

Two major base classes collection and map

In the class inheritance system of the collection framework, the topmost layer has two interfaces:

    • CollectionRepresents a set of pure data

    • MapRepresents a set of Key-value

Generally inherited from Collection or Map of a collection class, it provides two "standard" constructors:

    • constructor with no arguments, creating an empty collection class

    • Has a type with Collection the same constructor as the base class (or Map ), creating a new collection class with the same elements as the given parameter

Because the interface cannot contain constructors, the conventions of the above two constructors are not mandatory, but in the current collection framework, all inherited Collection or derived Map subclasses follow this convention.

Collection

Java-collection-hierarchy

As shown, the collection class has three main interfaces:

    • SetRepresents a collection where duplicate elements are not allowed (a collection that contains no duplicate elements)

    • ListRepresents a collection of allowed duplicate elements (an ordered collection (also known as a sequence))

    • QueueJDK1.5 is added, and the main difference between the above two collection classes is that Queue they are primarily used to store data instead of processing data. (A collection designed for holding elements prior to processing.)

Map

Mapclasshierarchy

Map is not a true set (is not true collections), but this interface provides three "set View" (collection views), so that you can manipulate them like the set of operations, as follows:

    • Consider the contents of the map as a set of keys (map ' s contents to be viewed as a set of keys)

    • Consider the contents of the map as a collection of values (map ' s contents to be viewed as a collection of value)

    • Consider the contents of the map as a collection of key-value mappings (map ' s contents to be viewed as a set of key-value mappings)

Summarize

Today first open, the back will be a series of dry goods, stay Tuned.

It should be noted that all future source analysis will be based on Oracle JDK 1.7.0_71, please be aware.

A collection framework for the Java Foundation

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.