Java integration framework, Collection interface, Set interface, List interface, Map interface, which are already commonly used implementation classes, simple JDK source code analysis underlying implementation, collection Collection framework

Source: Internet
Author: User

Java integration framework, Collection interface, Set interface, List interface, Map interface, which are already commonly used implementation classes, simple JDK source code analysis underlying implementation, collection Collection framework

(1) Collection framework:

Designers of the Java language have made some standards (interfaces) and implementations (classes that implement interfaces) on common data structures and algorithms ). All abstracted data structures and operations (algorithms) are collectively referred to as collection frameworks.

Programmers do not have to consider the data structure and algorithm implementation details when using specific applications. They only need to use these classes to create some objects and then apply them directly. This greatly improves the programming efficiency.

 

(2) Contents of the Collection framework:

 

 

 (3) interfaces of the Collection framework (specifications)

Collection interface: stores a group of unordered objects.

List interface: stores a group of non-unique, ordered objects

Set interface: stores a group of unique and unordered objects.

Map interface: stores a set of key-value objects and improves the ing between keys and values.

(4) Implementation classes of various interfaces:

Two common implementation classes of the List interface: ArrayList and List

ArrayList class: In its underlying code, there is actually an Object-type array, which can be resized using some methods. The length of the array itself cannot be changed during definition, the underlying source code of JDK is to create a new array with a longer length than the original one. copy each element of the original array, then, assign the first address of the new array to the reference of the original array to implement a variable-length array. Therefore, ArrayList features fast traversal and random access, the efficiency of inserting and deleting objects is low, because the underlying implementation is achieved through arrays.

Shortlist class: The underlying layer is implemented through nodes. It has two Node attributes, one is the root Node first and the other is the last Node. The added element is saved in the Node attribute item. See the following JDK source code, then, there are two attributes of "next" and "prev" respectively pointing to the last Node that saves the element and the Node that saves the element forward. In this way, a logical chain is formed, they are not consecutive in the heap memory. Therefore, the memory list class has the following features: the efficiency of accessing elements is not high, but the efficiency of inserting and deleting elements is high, because you can change the point through the node.

  The implementation class of Map only talks about a HashMap, and TreeMap does not talk about it, that is, there is a certain order when it is added, but it is not important at all, you can use the Collections class method to sort the collection elements.

At the underlying layer of HashMap is implemented through the node array (array + node). Why can't keys be repeated when the put () method stores keys and values? repeated values overwrite the original values, the key value at the underlying layer uses its hashCode to ensure uniqueness.

Let's also explain HashSet. Let's take a look at its JDK source code. map is a HashMap type and stores data in the map key. Therefore, the elements stored in HashSet cannot be repeated.

      

PRESENT is a constant.

Just talk about the set and get a general understanding of the underlying implementation principles for easy understanding and memory.

 

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.