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

Java中Comparable和Comparator实现对象比较

时间:2015-09-23 00:59:33      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

1.通过Comparable实现排序

技术分享
package Comparable;

import java.util.Arrays;

public class ComparableUser implements Comparable<ComparableUser> 
{

    private String id;
    private int age;

    public ComparableUser(String id, int age) {
        this.id = id;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int compareTo(ComparableUser o) {
         return this.age - o.getAge();
         //return ((ComparableUser) o).getAge() - this.age;
    }

    /**
     * 测试方法
     */
    public static void main(String[] args) {
        ComparableUser[] users = new ComparableUser[] {
                new ComparableUser("u1001", 25),
                new ComparableUser("u1002", 20),
                new ComparableUser("u1003", 21) };
        Arrays.sort(users);// 对对象进行排序
        for (int i = 0; i < users.length; i++) {
            ComparableUser user = users[i];
            System.out.println(user.getId() + " " + user.getAge());
        }
    }

}
技术分享

 

2.通过实现Comparator进行排序

技术分享
package Comparable;

import java.util.Arrays;
import java.util.Comparator;

class User {  
    
    private String id;  
    private int age;  
  
    public User(String id, int age) {  
        this.id = id;  
        this.age = age;  
    }  
  
    public int getAge() {  
        return age;  
    }  
  
    public void setAge(int age) {  
        this.age = age;  
    }  
  
    public String getId() {  
        return id;  
    }  
  
    public void setId(String id) {  
        this.id = id;  
    }  
}

public class UserComparator implements Comparator {  
      
    public int compare(Object arg0, Object arg1) {  
        return ((User) arg0).getAge() - ((User) arg1).getAge();  
    }  
  
    /** 
     * 测试方法 
     */  
    public static void main(String[] args) {  
        User[] users = new User[] { new User("u1001", 25),  
                new User("u1002", 20), new User("u1003", 21) };  
        Arrays.sort(users, new UserComparator());  
        for (int i = 0; i < users.length; i++) {  
            User user = users[i];  
            System.out.println(user.getId() + " " + user.getAge());  
        }  
    }

}  
技术分享

 

推荐使用案例:

技术分享
package Comparable;

import java.util.Arrays;
import java.util.Comparator;

class User {

    private String id;
    private int age;
    private int score;

    public User(String id, int age, int score) {
        this.id = id;
        this.age = age;
        this.setScore(score);
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

public class UserComparator implements Comparator<User> {

    public int compare(User arg0, User arg1) {
        if(arg0.getAge()<arg1.getAge())
            return 1;
        else if(arg0.getAge()>arg1.getAge())
            return -1;
        else if(arg0.getScore()<arg1.getScore())
            return -1;
        else if(arg0.getScore()>arg1.getScore())
            return 1;
        return 0;
    }

    /**
     * 测试方法
     */
    public static void main(String[] args) {
        User[] users = new User[] { 
                new User("u1001", 25, 90), new User("u1002", 20, 100), new User("u1003", 25, 91) };
        Arrays.sort(users, new UserComparator());
        for (int i = 0; i < users.length; i++) {
            User user = users[i];
            System.out.println(user.getId() + " " + user.getAge() + " " + user.getScore());
        }
    }

}
技术分享

 

Java中Comparable和Comparator实现对象比较

标签:

原文地址:http://www.cnblogs.com/nele/p/4830870.html

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