Java collection framework, collection interface, set interface, list interface, map interface, has been commonly used in their implementation class, simple JDK source code analysis of the underlying implementation

Source: Internet
Author: User

(i) Collection framework:

  The Java language designer has made some specifications (interfaces) and implementations (classes that implement interfaces) for commonly used data structures and algorithms. All abstract data structures and operations (algorithms) are collectively referred to as the collection framework.

Programmers in the application, do not have to consider the data structure and algorithm implementation details, only need to create some objects with these classes, and then directly applied to it, which greatly improve the efficiency of programming.

(ii) What the collection Framework contains:

(iii) interface of the collection Framework (specification)

Collection interface: stores a set of non-unique, unordered objects

List interface: stores a set of not unique, ordered objects

Set interface: stores a unique, unordered set of objects

Map interface: stores a set of key-value objects and increases the mapping of key (key) to value

(iv) Implementation classes for various interfaces:

2 Common implementation classes for the list interface:ArrayList and LinkedList

ArrayList class: in its underlying code, there is actually an object type array, through some methods to realize the expansion of the array, the length of the array itself can not be changed at the time of definition, the JDK source code is created by a new array, The length is longer than the original, copy each element of the previous original array, and then assign the first address of the new array to the reference of the original array, so that the variable-length array is implemented, so the ArrayList is characterized by fast traversal and random access, and low efficiency in inserting objects and deleting objects. Because the underlying implementation is implemented by arrays

LinkedList class: the bottom layer through the node to achieve, it has 2 node node attributes, a root node first, a tail node last. The added element is saved in the Node node properties item, look at the following JDK source code, and then via node there are 2 properties next and Prev point to the node of the last save element, and point to the previous save element, so as to form a chain of logic, They are not contiguous in heap memory, so the LinkedList class features: Access to elements is not efficient, but the insertion and deletion of elements is efficient, because the node change point can be achieved.

  The implementation class of map I just say a hashmap,treemap is not said, is added in the time, there is a certain order, but it is not important, you can use the tool class collections class method to implement the ordering of the collection elements.

the bottom of the HashMap is achieved by the node array (arrays + nodes), why the Put () method to save key and value, key can not be repeated, repeat value will overwrite the original value, The key value at the bottom is guaranteed to be unique through its hashcode .

the same to explain the hashset, to show you a source of its JDK, map is the HashMap type, the data stored in the map key, so that hashset stored elements can not be duplicated


Present is a constant.

   About the collection is only to talk about these, a general understanding of the underlying implementation principles can be, easy to understand and memory.

Java collection framework, collection interface, set interface, list interface, map interface, has been commonly used in their implementation class, simple JDK source code analysis of the underlying implementation

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: 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.