"One Minute Knowledge" HashSet and Treeset,hashmap and Hashtable

Source: Internet
Author: User
"One Minute Knowledge" HashSet and Treeset,hashmap and Hashtable



HashSet and TreeSet

HashSet

Hash table is implemented, the data in the HashSet is unordered.

There is no guarantee that the order of the elements may change.

It's not synchronized.

The collection element can be null but can only be placed in a null

When an element is deposited into the HashSet collection, HashSet calls the object's Hashcode () method to get the Hashcode value of the object, and then determines where the object is stored in Hashcode according to the HashSet value.

TreeSet

Binary tree implementation, the data in the TreeSet is automatically sorted, not allowed to put null values

TreeSet is the only implementation class for the SortedSet interface, and TreeSet ensures that the collection elements are in a sorted state.

TreeSet supports two sorting methods, natural sorting and custom sorting, where the natural sort is the default sort.

The object to be added to the treeset should be the same class.

TreeSet that two objects are not equal is two objects return false through the Equals method, or they do not return 0 through the CompareTo method comparison.


HashMap and Hashtable

HashMap

Thread not secure

Allow null keys and values to be more efficient

The method is not synchronize.

To provide external synchronization there are containsvalue and ContainsKey methods

HashMap is an implementation of map interface introduced by Java1.2

HashMap is a lightweight implementation of Hashtable

HashTable

Thread Safety

Null keys and values are not allowed

A little less efficient

Method is a contains method of synchronize

Hashtable inherits from the dictionary class,

Hashtable is older than HashMap.

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.