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 complexity
Basically, 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.UnsupportedOperationException
This 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-InheritanceCollection
Class array object. Therefore, the Framework provides a set of methods for the collection classes and arrays to convert each other andMap
As a collection.
Two basic classes: Collection and Map
In the class inheritance system of the Collection framework, the top layer has two interfaces:
Generally inherited fromCollection
OrMap
The Collection class will provide two "standard" Constructors:
-
A constructor without parameters creates an empty collection class.
-
There is a type and a base classCollection
OrMap
) 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, allCollection
OrMap
Sub-classes all follow this convention.
Collection
Java-collection-hierarchy
As shown in, the Collection class has three main interfaces:
-
Set
Indicates that A collection with duplicate elements is not allowed. that contains no duplicate elements)
-
List
An ordered collection (also known as a sequence ))
-
Queue
JDK1.5 is added, which is different from the above two collection classes in thatQueue
It 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)