Hashset,treeset and Linkedhashset

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, theAdd method returns False.
    • The set determines that two objects are the same, not using the = = operator, but according to the Equals method. That is, as long as two objects are returned True,set with the Equals method, the two objects will not be accepted.

HashSet
HashSet has the following characteristics

    • The order of elements cannot be guaranteed and the order may change
    • It's not synchronous.
    • The collection element can be null, but only one null

When an element is deposited into a hashset union, 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 based on the HashSet value.
Simply put, the hashset set determines that two elements are equal by two objects that compare equality by equals method, and two objects with the Hashcode () method return values equal
Note that if you want to put an object in HashSet and override the Equals method of the corresponding class for that object, you should also override its Hashcode () method. The rule is that if two objects return true through the Equals method, their hashcode should also be the same. In addition, the attribute used in the object as the equals comparison criterion should be used to calculate 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 maintains the order of the elements using the linked list.
    • When traversing the collection, Linkedhashset will access 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 the performance is slightly inferior to hashset when inserted.

TreeSet
TreeSet is the only implementation class for the SortedSet interface, and TreeSet ensures that the collection elements are in the sorted state. TreeSet supports two sorting methods, natural sorting and custom sorting, in which the natural sort is the default sorting method. The object that is added to the treeset should be the same class.

    • TreeSet determines that two objects are unequal by two objects returning false through the Equals method, or by comparing the CompareTo method without returning 0

Natural sort
Natural sorting uses the CompareTo (Object obj) method to sort the elements to compare the size relationships between elements, and then arranges the elements in ascending order.
Java provides a comparable interface that defines a CompareTo (object obj) method that returns an integer value that implements the object of the interface to compare the size. The Obj1.compareto (obj2) method returns 0, indicating 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 negative, obj1 is less than obj2. If we always return true for the Equals method of two objects, then the CompareTo method returned by the two objects should return a 0
Custom sorting
Natural sorting is based on 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

Transferred from: http://www.cnblogs.com/Terry-greener/archive/2011/12/02/2271707.html

Hashset,treeset and Linkedhashset

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.