180217_JAVA _ TreeSet stores classes containing multiple strings and sets sorting rules,

Source: Internet
Author: User

180217_JAVA _ TreeSet stores classes containing multiple strings and sets sorting rules,

The Person class is as follows:

1 class Person {2   String name;3   int age;4   String address;5 }

The main is as follows:

 1 import java.util.TreeSet; 2  3 public class Test{ 4   public static void main(String[] args){ 5     Person p1 = new Person("AA",18,"CN"); 6     Person p2 = new Person("BB",18,"JP"); 7     Person p3 = new Person("CC",18,"CN"); 8  9     TreeSet<Person> persons = new TreeSet<>();10     persons.add(p1);11     persons.add(p2);12     persons.add(p3);13     for (Object person : persons) {14       System.out.println(person);15     }16   }17 }

Because TreeSet is a sortedset, you must specify a sorting rule for the stored object (Person.

Sorting rules: ASC (ascending), age> address> name
Therefore, rewrite the compareTo () of Person ():

1 public int compareTo (Person o) {2 if (this. age> o. age) {3 return 1; 4} else if (this. age <o. age) {5 return-1; 6} 7 // determine the age. If the age is equal, the address 8 int x = this. address. compareTo (o. address); 9 if (x! = 0) {10 return x; 11} 12 // determines who the address is. If it is equal, determine name13 return this. name. compareTo (o. name); 14}

Note: To sort by DESC:

1. Switch 1/-1 in age comparison;
2. return inverse value in address comparison;
3. return inverse value in name comparison;

The logic sequence of the above Code:
Comparison between input comparison object o and itself;
If this. age> o. age, "1" is returned ";
If this. age <o. age, "-1" is returned ";
If equal, compare address;
Make x = this. address. compareTo (o. address );
If x! = 0, that is, not equal, x is the value to be returned (1/-1 );
If x = 0, that is, equal, compare name;
The value of this. name. compareTo (o. name) is the value to be returned:
If the value is 1, that is, greater,
If the value is-1, it is smaller,
If the value is 0, that is, all three are equal, they are completely equal and should not be saved.

 

Complete code:

1 package toBKY; 2 3 import java. util. treeSet; 4 5 public class Test {6 public static void main (String [] args) {7 Person p1 = new Person ("AA", 18, "CN "); 8 Person p2 = new Person ("BB", 18, "JP"); 9 Person p3 = new Person ("CC", 18, "CN "); 10 11 TreeSet <Person> persons = new TreeSet <> (); 12 persons. add (p1); 13 persons. add (p2); 14 persons. add (p3); 15 16 for (Object person: persons) {17 System. out. println (person ); 18} 19 // use for each less than Iterator Code 20 21} 22} 23 24 class Person implements Comparable <Person> {25 String name; 26 int age; 27 String address; 28 29 public Person (String name, int age, String address) {30 this. name = name; 31 this. age = age; 32 this. address = address; 33} 34 35 @ Override36 public String toString () {37 return "Person {" + 38 "name = '" + name +' \ ''+ 39 ", age = "+ age + 40", address = '"+ addr Ess + '\ ''+ 41'}'; 42} 43 44 @ Override45 public int compareTo (Person o) {46 if (this. age> o. age) {47 return 1; 48} else if (this. age <o. age) {49 return-1; 50} 51 // determines who is the age. If the age is equal, the address52 int x = this. address. compareTo (o. address); 53 if (x! = 0) {54 return x; 55} 56 // determines who the address is. If it is equal, determine name57 return this. name. compareTo (o. name); 58} 59}

Related Article

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.