Comparison of hashtable, hashmap, linkedhashmap, and treemap

Source: Internet
Author: User

Hashtable: inherits from the dictionary class and implements the map interface. The key or value is not allowed to be empty and the thread is synchronized;

Hashmap: inherits from the abstractmap class and implements the map interface. The key or value is null and the thread is not synchronized;

Linkedhashmap: inherits from the hashmap class, implements the map interface, allows the key or value to be empty, and stores the record insertion sequence, the thread is not synchronized.

Treemap: inherits from the abstractmap class and implements the sortedmap interface. It can sort by key or specify the sort comparator (implement the comparator interface ).

If you want map synchronization, you can use the synchronizedmap method in collections.

 

Note: If the result is stored in hashmap, it is very different from the sorting result in the database, because hashmap sorts the result by the hash value of its key. It is best to use linkedhashmap, it inherits hashmap and maintains a two-way linked list (specifically a circular linked list), which is used to record the current element, the previous element, and its next element, therefore, you can record the order in which it is added.

Comparison of hashtable, hashmap, linkedhashmap, and treemap

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.