码迷,mamicode.com
首页 > 编程语言 > 详细

java集合中对象某属性比较排序Comparable与Comparator

时间:2015-08-31 16:57:58      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

要对集合中的对象的某属性进行排序有两种方式。

a. 一种是要排序对象类实现comparable接口的compareTo方法;然后把对象放入list;然后调用Collections.sort(list);
b. 一种是不对要排序对象类做任何改动,创建Comparator接口的实现类C;然后 把对象放入list;然后调用Collections.sort(list, C);

 

a.eg

----------------------------------

 1 public class User implements Comparable<User>{
 2     private String name;
 3     private int age;
 4     private int gender;
 5     private String address;
 6     
 7     public String getName() {
 8         return name;
 9     }
10     
11     public void setName(String name) {
12         this.name = name;
13     }
14     
15     public int getAge() {
16         return age;
17     }
18     
19     public void setAge(int age) {
20         this.age = age;
21     }
22     
23     public int getGender() {
24         return gender;
25     }
26     
27     public void setGender(int gender) {
28         this.gender = gender;
29     }
30     
31     public String getAddress() {
32         return address;
33     }
34     
35     public void setAddress(String address) {
36         this.address = address;
37     }
38     
39     public User() {
40         super();
41     }
42     
43     public User(String name, int age, int gender, String address) {
44         super();
45         this.name = name;
46         this.age = age;
47         this.gender = gender;
48         this.address = address;
49     }
50    
51 
52     @Override
53     public int compareTo(User o) {
54         if(o!=null){
55             if(this.getAge()>o.getAge()){
56 
57                return 1;
58             }else if(this.getAge()==o.getAge()){
59 
60                return 0;
61 
62             }
63 
64        }
65         return -1;
66     }
67 }

 

-------------------------------------------main

 1 List<User> ulist = new ArrayList<User>();
 2 ulist.add(new User("wangbo",29,1,"长沙"));
 3 ulist.add(new User("wumei",44,1,"株洲"));
 4 ulist.add(new User("zhangjie",19,1,"岳阳"));
 5 ulist.add(new User("lihua",36,1,"长沙"));
 6 ulist.add(new User("zhangchangzhe",19,1,"衡阳"));
 7 
 8 Collections.sort(list);
 9 
10 for(User u:ulist){
11             System.out.println(u.getName()+"\t"+u.getAge()+"\t"+u.getAddress());
12 }
13 
14  

 

==============================================

b.eg

--------------------------------

 1 public class User {
 2     private String name;
 3     private int age;
 4     private int gender;
 5     private String address;
 6     
 7     public String getName() {
 8         return name;
 9     }
10     
11     public void setName(String name) {
12         this.name = name;
13     }
14     
15     public int getAge() {
16         return age;
17     }
18     
19     public void setAge(int age) {
20         this.age = age;
21     }
22     
23     public int getGender() {
24         return gender;
25     }
26     
27     public void setGender(int gender) {
28         this.gender = gender;
29     }
30     
31     public String getAddress() {
32         return address;
33     }
34     
35     public void setAddress(String address) {
36         this.address = address;
37     }
38     
39     public User() {
40         super();
41     }
42     
43     public User(String name, int age, int gender, String address) {
44         super();
45         this.name = name;
46         this.age = age;
47         this.gender = gender;
48         this.address = address;
49     }
50     
51 }
52 
53  

 

------------------------------

 1 import java.util.Comparator;
 2 
 3 public class ComparatorImpl implements Comparator<User>{
 4 
 5     @Override
 6     public int compare(User o1, User o2) {
 7         
 8         if(null!=o1 && null!=o2){
 9             if(o1.getAge() >o2.getAge()){
10                 return 1;
11             }else if(o1.getAge() ==o2.getAge()){
12                 return 0;
13             }
14         }
15         return -1;
16     }
17 
18 }

 

-----------------------------------main

 1 List<User> ulist = new ArrayList<User>();
 2         ulist.add(new User("wangbo",29,1,"长沙"));
 3         ulist.add(new User("wumei",44,1,"株洲"));
 4         ulist.add(new User("zhangjie",19,1,"岳阳"));
 5         ulist.add(new User("lihua",36,1,"长沙"));
 6         ulist.add(new User("zhangchangzhe",19,1,"衡阳"));
 7         Collections.sort(ulist, new ComparatorImpl());
 8         for(User u:ulist){
 9             System.out.println(u.getName()+"\t"+u.getAge()+"\t"+u.getAddress());
10         }

 

java集合中对象某属性比较排序Comparable与Comparator

标签:

原文地址:http://www.cnblogs.com/wdmwj626/p/4773265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!