在开发过程中如果需要对集合或数组中的一些属性左比较,并且按照一定的顺序排序的话,java的一个比价好的解决办法是用比较器,这里介绍两种
1.Comparable接口
比如在Person中需要对首先对age属性排序,当age一样大的时候按照score属性排序,看个案例
package com.wyw; /** * Created by wuyawei on 2017/5/16. * 比较器的用法实现Comparable接口,并且实现compareTo方法 */ public class Person implements Comparable<Person> { private String name; private int age; private float score; /** * 定义排序规则 * 当if判断为大于,并且返回大于0的数值说明前者大,否则相反 */ @Override public int compareTo(Person o) { /**首先按照age参数比较*/ if(this.age > o.getAge()){ return -1; }else if(this.age < o.getAge()){ return 1; } /**下来按照score参数比较*/ else{ if(this.score < o.getScore()){ return 1; } if(this.score > o.getScore()){ return -1; } return 0; } } public Person(String name, int age, float score) { this.name = name; this.age = age; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + '}'; } }
测试用例
package com.wyw; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; /** * Created by wuyawei on 2017/4/20. */ public class Test { public static void main(String[] args) throws Exception { Person person1 = new Person("张三",24,88.88f); Person person2 = new Person("李四",24,88.56f); Person person3 = new Person("王五",20,88.78f); Person person4 = new Person("赵六",20,88.99f); ArrayList<Person> arrayList = new ArrayList(4); arrayList.add(person1); arrayList.add(person2); arrayList.add(person3); arrayList.add(person4); Collections.sort(arrayList); for(Person p : arrayList){ System.out.println(p.toString()); } } }
输入结果
Person{name='张三', age=24, score=88.88}
Person{name='李四', age=24, score=88.56}
Person{name='赵六', age=20, score=88.99}
Person{name='王五', age=20, score=88.78}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2.Comparator是用来比较的,其实本质上跟Comparable差别不大
package com.comparo; /** * Created by wuyawei on 2017/5/16. */ public class User { private String name; private int age; public User() {} public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
测试用例跟比价器
package com.comparo; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * Created by wuyawei on 2017/5/16. */ public class UserComparator implements Comparator<User> { @Override public int compare(User user1, User user2) { /**判断两个参数是不是同一个对象*/ if(user1.equals(user2)){ return 0; }else if(user1.getAge()>user2.getAge()){ return 1; }else if(user1.getAge()<user2.getAge()){ return -1; /**判断当age值相等的时候*/ }else{ int f = user1.getName().compareTo(user2.getName()); if(f < 0){ return -1; } return 0; } } public static void main(String[] args) { List<User> userList = new ArrayList<>(); userList.add(new User("c",4)); userList.add(new User("b",4)); userList.add(new User("b",445)); userList.add(new User("d",445)); userList.add(new User("e",412)); userList.add(new User("c",41)); userList.add(new User("a",40)); userList.add(new User("a",4)); UserComparator userComparator = new UserComparator(); Collections.sort(userList,userComparator); for (User u : userList){ System.out.println(u.toString()); } } }
运行结果:
User{name='a', age=4}
User{name='b', age=4}
User{name='c', age=4}
User{name='a', age=40}
User{name='c', age=41}
User{name='e', age=412}
User{name='b', age=445}
User{name='d', age=445}
可以看出,是先进行年龄排序,然后根据name值排序,当然排序规则自定义
本节完