TreeSet ordering of unnatural order elements

Source: Internet
Author: User
Tags comparable

/*
1. When adding elements to TreeSet, if the elements themselves have the nature of the order, they are sorted by the nature of the element's natural order.

2. When adding elements to TreeSet, if the element itself does not have a natural order, then the class that the element belongs to must implement the comparable interface, the element
The comparison rule is defined on the CompareTo () method.

3. If the CompareTo method returns 0 when the element is compared, the element is treated as a repeating element and is not allowed to be added.
(Note: TreeSet does not have any relationship with the hashcode, Equals method.) )

4. When adding elements to TreeSet, if the element itself does not have a natural order of nature, and the class to which the element belongs does not implement the comparable interface,
Then you must pass in a comparer when you create the TreeSet.

5. When adding elements to TreeSet, if the element itself does not have a natural order of nature, and the class to which the element belongs has implemented the comparable interface,
When the TreeSet object is created, it is also passed to the comparator, which is preferred using the comparator's comparison rules.

How to customize a comparer: Customize a class to implement the comparator interface, and define the comparison rules between elements and elements within the Compare method.
*/

1 classEmployeeImplementsComparable<employee> {2 String name;3     intID;4     intsalary;5 6      PublicEmployee (String name,intIdintsalary) {7          This. Name =name;8          This. ID =ID;9          This. Salary =salary;Ten     } One  A @Override -      PublicString toString () { -         return"{name=" + name + ", id=" + ID + ", salary=" + Salary + "}"; the     } -  - @Override -      Public intCompareTo (Employee e) {//a negative integer, 0, or positive integer, depending on whether the object is less than, equal to, or greater than the specified object.  +         return  This. Salary-e.salary; -     } + } A  at //Customizing a comparer - classMycomparatorImplementsComparator<employee> { - @Override -      Public intCompare (employee O1, employee O2) { -         returnO1.id-o2.id; -     } in } -  to  Public classDemo6 { +      Public Static voidMain (string[] args) { -         //with comparable interface, this time compare salary theTreeSet tree =NewTreeSet (); *Tree.add (NewEmployee ("Jay", 1, 1000)); $Tree.add (NewEmployee ("Lee", 4, 3000));Panax NotoginsengTree.add (NewEmployee ("MJ", 2, 2000)); -Tree.add (NewEmployee ("JK", 3, 500)); the  +SYSTEM.OUT.PRINTLN ("Use comparable interface, compare salary at this time"); AIterator it =tree.iterator (); the          while(It.hasnext ()) { + System.out.println (It.next ()); -         } $  $System.out.println ("*******************"); -  -         //with comparator comparator, compare ID at this time theMycomparator Comparator =NewMycomparator ();//Create a Comparer object -TreeSet tree2 =NewTreeSet (Comparator);//incoming comparer when creating TreeSetWuyiTree2.add (NewEmployee ("Jay", 1, 1000)); theTree2.add (NewEmployee ("Lee", 4, 3000)); -Tree2.add (NewEmployee ("MJ", 2, 2000)); WuTree2.add (NewEmployee ("JK", 3, 500)); -  AboutSYSTEM.OUT.PRINTLN ("Use comparator comparator, compare ID at this time"); $Iterator it2 =tree2.iterator (); -          while(It2.hasnext ()) { - System.out.println (It2.next ()); -         } A     } +  the}

The result is:

with comparable interface, at this time compare Salary{name=jk, id=3, salary=500}{name =jay, id=1, salary=1000}{name = MJ, id=2, salary=2000}{name=lee, id=4, salary=3000}******************* with comparator comparator, compare ID at this time {Name=jay, id=1, salary=1000}{name=mj, id=2, salary=2000}{name=jk, id=3, salary=500 }{name=lee, id=4, salary=3000}

TreeSet ordering of unnatural order elements

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.