Overview of the Java Collection framework

Source: Internet
Author: User
Tags array length

Recently was on the ground successively asked several times HashMap realization, the answer is not good, intends to review the JDK in the collection framework, and try to analyze its source code, so on the one hand, these classes are very practical, master its implementation can better optimize our program On the other hand, learning from the JDK is how to achieve such a set of elegant and efficient class library, improve the programming ability.

Before introducing the specific fit class, this article gives a general description of the set framework in Java, looking at the framework from a high angle, and understanding some of the concepts and conventions of this framework, which will greatly help to analyze a specific class later, let's start.

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)
Implementation of the collection (Collection implementations)

The class that implements the collection interface generally follows <实现方式>+<接口> the naming method, and the generic collection implementation class is as follows:

Interface Hash Table resizable Array Balanced Tree Linked List Hash Table + Linked List
Set HashSet TreeSet Linkedhashset
List ArrayList LinkedList
Deque Arraydeque LinkedList
Map HashMap TreeMap Linkedhashmap
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.

$ java -versionjava version "1.7.0_71"Java(TM) SE Runtime Environment (build 1.7.0_71-b14)Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
Reference
    • Http://docs.oracle.com/javase/7/docs/technotes/guides/collections/overview.html
    • Https://en.wikipedia.org/wiki/Java_collections_framework

I have a public number that often shares some of the dry stuff related to Java technology, and if you like my share, you can use the search for "Java leader" or "Javatuanzhang" for attention.

Overview of the Java Collection framework

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.