Java TreeSet Applications

Source: Internet
Author: User

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

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.