The bottom implementation of HASHMAP, Linkedhashmap, Concurrenthashmap, ArrayList and LinkedList

Source: Internet
Author: User
the difference between HashSet and HashMap

HASHMAP implements the Map interface
HashSet implements the set interface

HashMap Store key value pairs
HashSet Only Store objects

HashMap use the Put () method to place elements in a map
HashSet use the Add () method to put elements into set

Use the Key object in HashMap to compute the hashcode value
HashSet uses member objects to compute hashcode values

HashMap is faster because it uses a unique key to get the object

HashSet more slowly than HashMap.


the difference between Hashtable and HashMap

The Hashtable method is synchronized
HashMap method is not synchronized

Hashtable based on Dictionary class
HashMap based on Abstractmap and ABSTRACTMAP implementation based on map interface

Key and value are not allowed to be null in Hashtable, NULL is encountered, return directly nullpointerexception
The key and value in HashMap are allowed to be null, and when the key is null, the Putfornullkey method is invoked to process and the value is not processed

The default size of the hash array in Hashtable is 11, and the expansion method is old*2+1
The default size of the hash array in HashMap is 16, and it must be the 2 index. What is ArrayList

ArrayList can be understood as a dynamic array whose capacity can grow dynamically, which refers to the size of the array used to store the list elements, and the capacity automatically increases as elements are added to the ArrayList
ArrayList allows all elements, including NULL, to be included
ArrayList is an asynchronous implementation of the list interface
ArrayList is orderly.

The definition is as follows:

public class Arraylist<e> extends abstractlist<e> implements List<e>, Randomaccess, Cloneable, java.io.serializable{}

ArrayList implements the list interface, the bottom layer uses the array to save all elements, its operation basically is the operation of the array

ArrayList inherits the Abstractlist abstract class, which is an array queue that provides related additions, deletions, modifications, and traversal functions.

ArrayList implementation of the Randmoaccess interface, which provides a random access function, randmoaccess is used in Java to be List implementation, to provide fast access to the list, we can through the sequence of elements quickly get the element object, which is fast random access

ArrayList implements the Cloneable interface, which covers the function clone () and can be cloned
ArrayList implements the Java.io.Serializable interface, meaning ArrayList supports serialization what is LinkedList

LinkedList implementation of list interface based on linked list
LinkedList allows all elements, including NULL, to be included
LinkedList is orderly.
LinkedList is the difference between Fail-fast's LinkedList and ArrayList

LinkedList Bottom is doubly linked list
ArrayList the bottom is a mutable array

LinkedList does not allow random access, that is, low query efficiency
ArrayList allows random access, that is, high query efficiency

LinkedList inserts and deletes efficiency quickly
Low ArrayList insertion and deletion efficiency

Explain:

For randomly accessed two methods, get and set,arraylist are better than LinkedList because LinkedList moves the pointer
Add and Remove,linedlist are more advantageous for adding and removing two methods because ArrayList want to move the data what is Concurrenthashmap

Concurrenthashmap of the map interface based on the double group and the linked list
The key of the element in the Concurrenthashmap is unique and the value is repeatable
Concurrenthashmap is not allowed to use null values and NULL keys
Concurrenthashmap is disorderly why use Concurrenthashmap

We all know that HashMap is not thread safe, when we have only one thread in the use of hashmap, naturally there will be no problem, but if more than one thread involved, and read the write process, the HashMap will fail-fast. To solve the problem of HashMap synchronization, our solution has

Hashtable
Collections.synchronizedmap (HASHMAP)
These two methods are basically the entire hash table structure plus synchronous lock, so in the lock table, other threads will need to wait, no doubt the performance is not high, so we introduce Concurrenthashmap, both synchronous and can be multiple-thread access Data structure of Concurrenthashmap

Concurrenthashmap's data structure is a segment array, segment's data structure is an array of hashentry, and Hashentry is our key-value pair, which can form a linked list. It's easy to understand that the HashMap in the array

From the structure above, we can see that the process of locating an element requires two hash operations, the first hash to the segment, and the second hash to the head of the linked list where the element is located, so the Concurrenthashmap The side effect of this kind of structure is that the process of the hash is longer than the ordinary hashmap, but the benefit is that the writing operation can only add the lock to the segment of the element, and will not affect the other segment. Precisely because of its internal structure and mechanism, CONCURRENTHASHMAP has a much higher performance than the Hashtable and HashMap after the synchronization package. Ideally, Concurrenthashmap can support 16 threads to perform concurrent writes (if the concurrency level is set to 16), and read operations for any number of threads

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.