java中compareTo和compare方法之比较,集合中对象的比较

前言

转自:http://www.cnblogs.com/yueliming/archive/2013/05/22/3092576.html

(这里做了一些小改动)

一直一来对集合中对象的比较方案,有些模糊,这里做些总结:

有两个方法可以实现:

1. 让 Student 实现Comparable接口:

compareTo(Object o)方法是java.lang.Comparable<T>接口中的方法,当需要对某个类的对象进行排序时,该类需要实现Comparable<T>接口的,必须重写public int compareTo(T o)方法。

2. 实例化一个比较器:

compare(Object o1,Object o2)方法是java.util.Comparator<T>接口的方法,它实际上用的是待比较对象的compareTo(Object o)方法。

注意:两者对于比较此对象与指定对象的顺序:如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数:

返回整数,1,-1,0;返回1表示大于,返回-1表示小于,返回0表示相等。

代码

1. 实现Comparable接口:

package com.horizon.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Student implements Comparable {

    private int id;
    private int age;
    private String name;

    public Student(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static void main(String args[]) {
        List<Student> list = new ArrayList<Student>();
        for (int i = 1000; i > 0; i--) {
            list.add(new Student(i));
        }

        Collections.sort(list);

        for (Student s : list) {
            System.out.println(s.getId());
        }
    }

    public int compareTo(Object o) {
        if (o instanceof Student) {
            Student s = (Student) o;
            if (this.id > s.id) {
                return 1;
            } else {
                return 0;
            }
        }
        return -1;
    }

}

2. 实例化一个比较器MenuComparator:

package com.horizon.action;

import java.util.Comparator;

public class MenuComparator implements Comparator {

    public int compare(Object o1, Object o2) {
        if (null != o1 && null != o2) {
            Menu menu1 = (Menu) o1;
            Menu menu2 = (Menu) o2;
            if (menu1.getId() > menu2.getId()) {
                return 1;
            } else {
                return 0;
            }
        }
        return 0;
    }

}
package com.horizon.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Menu {

    private int id;
    private int age;
    private String name;

    public Menu(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static void main(String args[]) {
        List<Menu> list = new ArrayList<Menu>();
        for (int i = 1000; i > 0; i--) {
            list.add(new Menu(i));
        }

        for (Menu m : list) {
            System.out.println(m.getId());
        }
        Collections.sort(list,new MenuComparator());

        for (Menu m : list) {
            System.out.println(m.getId());
        }
    }
}

3.两个合体:

package com.horizon.action;

public class User implements Comparable<Object> {
    int id;
    String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    /*
     * Getters and Setters
     */
    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int compareTo(Object o) {
        if (this == o) {
            return 0;
        } else if (o != null && o instanceof User) {
            User u = (User) o;
            if (id <= u.id) {
                return -1;
            } else {
                return 1;
            }
        } else {
            return -1;
        }
    }
}
package com.horizon.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {
    // 编写Comparator,根据User的id对User进行排序
    private static final Comparator<User> COMPARATOR = new Comparator<User>() {
        public int compare(User o1, User o2) {
            return o1.compareTo(o2);// 运用User类的compareTo方法比较两个对象
        }
    };

    public static void main(String[] args) {
        List<User> student = new ArrayList<User>();
        User user1 = new User(1, "yueliming");
        User user2 = new User(2, "yueliming");
        student.add(user2);
        student.add(user1);
        Collections.sort(student, COMPARATOR);// 用我们写好的Comparator对student进行排序
        for (int i = 0; i < student.size(); i++) {
            System.out.println(student.get(i).getId());
        }
    }
}

java中compareTo和compare方法之比较,集合中对象的比较,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。