This article mainly introduces the implementation class TreeSet of the more important set interfaces in the Java collection.
TreeSet class, the data structure of the bottom with binary tree
* Inserts and extracts elements in a collection in an orderly manner.
* Elements added to TreeSet must be sortable
* Ensure the uniqueness of the data:
* First: Let the added class itself be comparable,
* Implement the CompareTo () method in the comparable interface
First create an added type, as defined below, to make the element itself comparable
* Add class in implement comparable method in CompareTo () method
* Sorted by the age of the objects stored, and the same attributes of the object culling
Then create a class that creates the TreeSet collection in the main method inside, and adds the Wimen class object
The result diagram is as follows. You can see that the TreeSet collection calls Add () internally and automatically calls the CompareTo () method that adds the object.
The age is sorted and the objects of the same attribute are also rejected.
This essay only implements the first method, that is, to add the elements themselves have a comparison, in the next article I implement the second method, let the TreeSet collection instantiation of the parameters, and make the collection of the comparison
Java TreeSet Applications