HashSet, Linkedhashset, treeset differences

Source: Internet
Author: User

set Interface
Set does not allow the same element to be included, and if an attempt is made to add two identical elements to the same collection, the Add method returns False.
The set judges that two objects are the same instead of using the = = operator, but based on the Equals method. That is, the two objects are not accepted as long as two objects are returned using the Equals method comparison True,set.

HashSet
HashSet has the following characteristics
 Can not guarantee the order of elements, the order may be changed
 Is not synchronized.
 The collection element can be null, but can only be placed in a null
When you deposit an element into a hashset binding, 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.
To put it simply, the HashSet set determines that two elements are equal, and that two objects are equal through the Equals method, and that the Hashcode () method of two objects returns the same value
Note that if you want to put an object into HashSet, overriding the Equals method of the corresponding class of the object, you should also override its Hashcode () method. The rule is that if two objects return true through the Equals method comparison, their hashcode should also be the same. In addition, an object that is used as the equals comparison standard should be used to compute the value of the hashcode.

Linkedhashset
The Linkedhashset collection also determines where the element is stored based on the hashcode value of the element, but it also uses a linked list to maintain the order of the elements. This makes the elements appear to be saved in the insertion order, that is, when the collection is traversed, linkedhashset accesses the elements of the collection in the order in which they are added.
Linkedhashset performance is better than hashset when iterating through all the elements in the set, but performance is slightly inferior to hashset at the time of insertion.

The

TreeSet class
TreeSet is the only implementation class for the SortedSet interface, 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 by CompareTo method comparison does not return 0
Natural sort
Natural sort uses the CompareTo of the element to be sorted (object obj) method to compare the size relationships between elements, and then arrange the elements in ascending order.
Java provides a comparable interface that defines a CompareTo (object obj) method that returns an integer value that can be compared to the object that implements the interface. The
Obj1.compareto (obj2) method, if returned 0, indicates that the two objects being compared are equal, and if a positive number is returned, the OBJ1 is greater than obj2 and, if it is a negative number, the obj1 is less than obj2.
If we always return the Equals method of two objects to true, the CompareTo method return of the two objects should return 0
custom sort
The natural sort is sorted by the size of the collection elements in ascending order, if you want to customize the sort, You should use the comparator interface to implement the int compare (T o1,t O2) method

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.