一、Comparable接口
类直接实现Comparable接口,并实现compareTo方法
package cn.lxx.test;
public class User implements Comparable<User> { //该类实现Comparable接口,参数为User
private String name;
private int age;
private boolean sex;
public User() {
}
public User(String name, int age, boolean sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
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 boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
/**
* 重写compareTo方法,实现年龄升序,年龄相同则姓名升序
**/
@Override
public int compareTo(User user) {
if (this.age == user.age) return this.name.compareTo(user.name);
return this.age - user.age; //将this想像成一排不变的对象(已经按照要求排好序的),而User就是当前要插入的对象,只有user属性小于this属性才插入从而升序,个人理解,希望有所帮助
}
}
class Test {
public static void main(String[] args) {
User user1 = new User("ake", 25, true);
User user2 = new User("reo", 24, false);
User user3 = new User("fg", 24, false);
List<User> list = new ArrayList<User>();
list.add(user1);
list.add(user2);
list.add(user3);
Collections.sort(list);
System.out.println("Comparable:" + list);
}
}
二、Comparator接口
两种方式:一种是类直接实现接口,并实现compare方法(不推荐,强耦合);另一种是匿名内部类实现接口,并实现compare方法。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class User implements Comparator<User> {
private String name;
private int age;
private boolean sex;
public User() {
}
public User(String name, int age, boolean sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
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 boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
@Override
public int compare(User o1, User o2) {
return o1.age-o2.age;
}
}
class Test{
public static void main(String[] args) {
User user1=new User("dingli",25,true);
User user2=new User("huxiaojuan",24,false);
User user3=new User("xxx",24,false);
List<User> list=new ArrayList<User>();
list.add(user1);
list.add(user2);
list.add(user3);
// 方式1:
Collections.sort(list, new User()); //类实现了的Comparator能满足需求
System.out.println("类自身实现Comparator:"+list);
// 方式2:
Collections.sort(list, new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
return o1.getName().compareTo(o2.getName()); //按照名字升序
}
});
System.out.println("匿名内部类方式:"+list);
}
}
三、比较两个接口
Comparable是内比较器,Comparator是外比较器。即实现Comparable接口的类,具备对象之间的直接比较;实现Comparator接口的类,不具备对象之间的直接比较。
public static <T extends Comparable<? super T>> void sort(List<T> list)
public static <T> void sort(List<T> list, Comparator<? super T> c)
标签:Java,name,age,list,比较,User,sex,public
From: https://www.cnblogs.com/wanglei5205/p/16886879.html