Java Collection framework Overview

Source: Internet
Author: User

Java Collection framework Overview

Recently, I was asked several times about the implementation of HashMap, but the answer is not good. I plan to review the collection framework in JDK and try to analyze its source code. On the one hand, these classes are very practical, understanding its implementation can better optimize our program. On the other hand, we will learn how JDK has implemented such an elegant and efficient class library to improve programming capabilities.

Before introducing specific applicable classes, this article gives a rough description of Java's collection framework. It looks down on this framework from a high perspective and understands some ideas and conventions of this framework, this will greatly help you analyze a specific class later. Let's get started.

Collection framework (collections framework)

First of all, it should be clear that the set represents a group of objects and arrays, but the array length cannot be changed, but the set can ). The Collection framework in Java defines a set of specifications for representation and operation sets, decoupling specific operations from implementation details.

To put it bluntly, we can regard a collection as a micro-database, and the operations are nothing more than "add, delete, modify, and query". When learning to use a specific set class, we need to take these four operationsTime-Space complexityBasically, you can master this class.

Design Concept

The main idea is to provide a set of "small but beautiful" APIs. The API needs to be friendly to programmers. When new functions are added, programmers can get started quickly.
To ensure that the core interface is small enough, the top-level interface, that is, the Collection and Map interfaces, does not determine whether the set is mutability or not, and whether the modifiability can be changed), whether the size of resizability can be changed) these nuances. On the contrary, some operations are optional and are thrown during implementation.UnsupportedOperationExceptionThis indicates that the set does not support this operation. The implementer of the set must declare in the document that these operations are not supported.

To ensure that the top-level Core interfaces are small enough, they can only contain the following methods:

In addition, all collection classes must provide friendly interactive operations, including non-InheritanceCollectionClass array object. Therefore, the Framework provides a set of methods for the collection classes and arrays to convert each other andMapAs a collection.

Two basic classes: Collection and Map

In the class inheritance system of the Collection framework, the top layer has two interfaces:

  • CollectionIndicates a group of pure data

  • MapIndicates a group of key-value pairs.

Generally inherited fromCollectionOrMapThe Collection class will provide two "standard" Constructors:

  • A constructor without parameters creates an empty collection class.

  • There is a type and a base classCollectionOrMap) The same constructor creates a new collection class with the same elements as the given parameter.

Because the interface cannot contain constructors, the above two constructor Conventions are not mandatory, but in the current collection framework, allCollectionOrMapSub-classes all follow this convention.

Collection

Java-collection-hierarchy

As shown in, the Collection class has three main interfaces:

  • SetIndicates that A collection with duplicate elements is not allowed. that contains no duplicate elements)

  • ListAn ordered collection (also known as a sequence ))

  • QueueJDK1.5 is added, which is different from the above two collection classes in thatQueueIt is mainly used to store data rather than process data. A collection designed for holding elements prior to processing .)

Map

MapClassHierarchy

Map is not a set in the true sense are not true collections), but this interface provides three "collection views"), so that they can be operated like a set, the details are as follows:

  • Regard the content of map as the set of keys map's contents to be viewed as a set of keys)

  • Regard the content of map as the value set map's contents to be viewed as a collection of values)

  • Regard the content of map as a set of key-value ing map's contents to be viewed as a set of key-value mappings)

Summary

Today, let's start with a series of dry goods, Stay Tuned.

Please note that in the future, all source code analysis will be based on Oracle JDK 1.7.0 _ 71.

$ Java-version
Java 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)

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.