Treeset ()

Source: Internet
Author: User
Tags comparable

Treeset () Class

The treeset () class implements the set interface, which is supported by the treemap instance. This class ensures that sorted sets arrange elements in ascending order, and mayNatural orderSort (seeComparable), Or sort by the comparator provided when the set is created.TreesetThe instance will use itsCompareto(OrCompare) Method to execute all key comparisons. Therefore, from the set perspective, this method considers the two keys that are equal to each other to be equal. Even if the order of the set is different from the equal sign, its behavior is alsoYesWell defined; it just violatesSetThe common protocol of the API.

1

PublicTreeset()

Construct a new empty set, which is sorted by the natural order of elements. All elements inserted into the set must implement the comparable interface.

public boolean add(E o)

When an element is added, the comparable API is called to compare the elements. The O element is added to the corresponding position and maintained in ascending order.

2

Rewrite class, implement the comparator interface, and implement the compare method internally;

PublicTreeset(Comparator <? Super E> C)

Creates a new empty set, which is sorted by the specified comparator. All elements inserted into the set must be compared by the specified comparator.

Related Keywords:

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.