Differences between hashset and treeset

Source: Internet
Author: User
Tags set set

Set Interface

There is no obvious order between multiple objects in the Set set. For details, refer to the API documentation (it can be seen how important it is to include the API documentation at any time), which is basically the same as the collection method. But the behavior is different (set cannot contain duplicate elements ).
The Set set does not allow repeated elements because set judges that two objects are the same instead of using the = Operator, but based on the equals method. That is, if two objects are compared using the equals method and return true, the set object cannot accept two objects.

Hashset class
Hashset stores the elements of a set based on the hash algorithm, so it has good access and search performance.
Hashset features:
(1) hashset is not synchronous. To access multiple threads, you must use the code to ensure synchronization.
(2) The set element value can be null.

Treeset class
Treeset is the only implementation of the sortedset interface. treeset can ensure that the elements in the set are sorted (the elements are ordered ).
Treeset provides several additional methods:
Comparator comparator (): returns the comparator used by the current set, or returns NULL, indicating that the comparator is sorted in a natural way.
Object first (): returns the first element in the set.
Object last (): returns the last element in the set.
Objiect lower (Object E): return the elements in the set that are located before the specified Element (that is, elements smaller than the maximum element of the specified element, the reference element can be not a treeset element ).
Object higher (Object E): returns the elements located after the specified Element in the collection (that is, the minimum element greater than the specified element. The reference element does not need the treeset element ).
Sortedset subset (fromelement, toelement): returns a subset of this set, ranging from fromelement (including greater than or equal to) to toelement (excluding less ).
Sortedset headset (toelement): returns a subset of this set, which consists of elements smaller than toelement.
Sortedset tailset (fromelement): returns a subset of this set, which consists of elements greater than or equal to fromelement.

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.