There is the person class as follows:
1 class Person {2String name; 3 int Age ; 4 String address; 5 }
There are main as follows:
1 ImportJava.util.TreeSet;2 3 Public classtest{4 Public Static voidMain (string[] args) {5person P1 =NewPerson ("AA", "CN");6person P2 =NewPerson ("BB", "JP");7Person P3 =NewPerson ("CC", +, "CN");8 9treeset<person> persons =NewTreeset<>();Ten Persons.add (p1); One Persons.add (p2); A Persons.add (p3); - for(Object person:persons) { - System.out.println (person); the } - } -}
Because TreeSet is a sortable collection, you specify a collation for the holding object (person).
Collation: ASC (Ascending), age > Address > Name
So rewrite the person's CompareTo ():
1 Public intcompareTo (Person o) {2 if( This. Age >o.age) {3 return1;4}Else if( This. Age <o.age) {5 return-1;6 }7 //judge The age who is big, if equal, then judge the address8 intx = This. Address.compareto (o.address);9 if(x! = 0){Ten returnx; One } A //judge the address who is big, if equal, then judge the name - return This. Name.compareto (o.name); -}
Note: to sort desc:
1, the age comparison in exchange 1/-1;
2. Return the opposite value in address comparison;
3, the name comparison to return the opposite value;
The logical order of the above code:
The incoming comparison object o is compared with itself;
If This.age > O.age, return "1";
If This.age < O.age, return "-1";
If equal, compare address;
Make x = This.address.compareTo (o.address);
if x! = 0, which is not equal, X is the value to return (1/-1);
if x = = 0, i.e. 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 than,
If the value is-1, which is less than,
If the value is 0, that is equal to all three, it is exactly equal and should not be deposited.
Full code:
1 PackageTobky;2 3 ImportJava.util.TreeSet;4 5 Public classtest{6 Public Static voidMain (string[] args) {7person P1 =NewPerson ("AA", "CN");8person P2 =NewPerson ("BB", "JP");9Person P3 =NewPerson ("CC", +, "CN");Ten Onetreeset<person> persons =NewTreeset<>(); A Persons.add (p1); - Persons.add (p2); - Persons.add (p3); the - for(Object person:persons) { - System.out.println (person); - } + //use for each less than iterator code - + } A } at - classPersonImplementsComparable<person>{ - String name; - intAge ; - String address; - in PublicPerson (String name,intAge , String address) { - This. Name =name; to This. Age =Age ; + This. Address =address; - } the * @Override $ PublicString toString () {Panax Notoginseng return"Person{" + -"Name=" + name + ' \ ' + the", age=" + Age + +", address= ' + address + ' \ ' + A‘}‘; the } + - @Override $ Public intcompareTo (Person o) { $ if( This. Age >o.age) { - return1; -}Else if( This. Age <o.age) { the return-1; - }Wuyi //judge The age who is big, if equal, then judge the address the intx = This. Address.compareto (o.address); - if(x! = 0){ Wu returnx; - } About //judge the address who is big, if equal, then judge the name $ return This. Name.compareto (o.name); - } -}
180217_java learn to store classes with multiple strings in _treeset and set collations