180217_JAVA学习_TreeSet中存放含多个String的类…

2018-06-18 03:17:08来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

有Person类如下:

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

有main如下:

 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 }

由于TreeSet为可排序集合,所以要为存放对象(Person)指定排序规则。

排序规则:ASC(升序),age > address > name
故重写Person的compareTo():

 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     //判断age谁大,若相等,则判断address
 8     int x = this.address.compareTo(o.address);
 9     if(x != 0){
10         return x;
11     }
12     //判断address谁大,若相等,则判断name
13     return this.name.compareTo(o.name);
14 }

注:若要DESC排序:

  1、age比较中交换1/-1;
  2、address比较中return相反数值;
  3、name比较中return相反数值;

上述代码的逻辑顺序:
  传入比较对象o与自身进行对比;
    若this.age > o.age,返回“1”;
    若this.age < o.age,返回“-1”;
    若相等,比较address;
  令x = this.address.compareTo(o.address);
    若x != 0,即不相等,x即为要返回的值(1/-1);
    若x == 0,即相等,比较name;
  this.name.compareTo(o.name)的值即为要返回的值:
    若值为 1,即大于,
    若值为-1,即为小于,
    若值为 0,即三处均相等,则完全相等,不应存入。

 

完整代码:

 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         //使用for each 比Iterator代码量少
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     @Override
36     public String toString() {
37         return "Person{" +
38             "name='" + name + '\'' +
39             ", age=" + age +
40             ", address='" + address + '\'' +
41          '}';
42   }
43 
44   @Override
45   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     //判断age谁大,若相等,则判断address
52     int x = this.address.compareTo(o.address);
53     if(x != 0){
54       return x;
55     }
56     //判断address谁大,若相等,则判断name
57     return this.name.compareTo(o.name);
58   }
59 }

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:List接口、Set接口和Map接口

下一篇:hibernate框架学习笔记8:一对多关系案例