Some gossip about the Java collection class

Source: Internet
Author: User

The relationship between Map and set

Map and set are really good friends, there are countless links.

Map can be seen as an extension of set, because when the contents of a set are stored in the form of a key-value value pair, the set can actually be used as a map. In turn, the map actually has a map.entry internal interface, and map in the value pair, regardless of value, and only consider the key, so map can also be regarded as a set of key, and value is only the appendage of key.

HashMap the element is added, the value is then hashed according to the hashcode of the key to determine the location of the store. HashMap the bottom of the array entry[], according to the hash value to determine which element of the key-value, and entry[] is actually a table.

the bottom of the hashset is the HashMap to achieve . The HashSet bottom uses HashMap to hold all the elements as HashMap keys, and the corresponding value is a static final object named present. Methods for hashset operations are called HashMap methods. If the element is duplicated, the hashcode () and Equals () of the element object are judged at the same time, hashcode is equal and equals is returned true to be considered as a repeating element without substitution.

The relationship between TreeSet and TreeMap is also very similar, that is, the treeset underlying is treemap stored. TreeMap uses a sort binary tree of red and black trees to hold each entry (tree node) in the map.

List of three implementations: Arraylist,vector and LinkedList.

Vector and a son. Stack,stack just added 5 methods on the basis of the vector, and the code of just five methods turned the vector into a stack,stack still a vector, It inherits the synchronized lineage of vectors, which are thread-safe. Starting with JDK1.6, Java provides the Deque interface and provides implementation class Arraydeque, even if a data structure such as a stack is required in a program, and it is not recommended to use a stack for deque unless thread safety is required. Deque is a double-ended queue, which is a queue but also has a stack, the bottom is an array implementation.

Vector is almost replaced by ArrayList, and its only benefit is thread safety. Now even for thread safety and without vectors, a generic ArrayList can be packaged into thread-safe ArrayList through the Synchronizedlist () method of the Collections tool class.

ArrayList and LinkedList

ArrayList the bottom layer is based on the implementation of the array, so ArrayList when the creation of an initial capacity, provides a construction method, the programmer can create ArrayList when the initial capacity, if not explicitly provided capacity, Then the default setting for the program is 10. LinkedList is a two-way linked list storage structure, not only implements the list interface, but also realizes the Deque dual-terminal queue interface.

Some gossip about the Java collection class

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.