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 the HashSet collection, HashSet calls the object's Hashcode () method to get the Hashcode value of the object, and then determines the location of the object 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.
TreeSet class
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.
Most Important:
1, TreeSet is two difference tree implementation, TreeSet in the data is automatically sequenced, not allowed to put null values.
2, HashSet is a hash table implementation, the data in HashSet is unordered, can be put into null, but only a null, the values in both cannot be duplicated, such as the unique constraints in the database.
3, HashSet required to put the object must implement the Hashcode () method, put the object, is the Hashcode code as the identity, and the same content of the string object, Hashcode is the same, so the content can not be duplicated. However, objects of the same class can be placed in different instances.
The difference between Java--hashset and TreeSet