Comparable接口
此接口只包含一个compareTo(T t)方法。
这个方法可以个给两个对象排序。返回值类型为int,它返回负数,0,正数来表明已经存在的对象小于,等于,大于输入对象。
此方法一般写在传入的集合元素中,让元素继承此接口,重写上述方法。例子如下:
package com.qianfeng.weekendDay03;
public class Student implements Comparable<Object>{
private String number;
private String name;
private int age;
private String e_mail;
private String phone;
public Student() {
super();
}
public Student(String number, String name, int age, String e_mail, String phone) {
super();
this.number = number;
this.name = name;
this.age = age;
this.e_mail = e_mail;
this.phone = phone;
}
@Override
public String toString() {
return "Student [学号为:" + number + ", 姓名为:" + name + ", 年龄为:" + age + ", e_mail为:" + e_mail + ", 手机号为:"
+ phone + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((e_mail == null) ? 0 : e_mail.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((number == null) ? 0 : number.hashCode());
result = prime * result + ((phone == null) ? 0 : phone.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (e_mail == null) {
if (other.e_mail != null)
return false;
} else if (!e_mail.equals(other.e_mail))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (number == null) {
if (other.number != null)
return false;
} else if (!number.equals(other.number))
return false;
if (phone == null) {
if (other.phone != null)
return false;
} else if (!phone.equals(other.phone))
return false;
return true;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
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 String getE_mail() {
return e_mail;
}
public void setE_mail(String e_mail) {
this.e_mail = e_mail;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
//重写比较器方法
@Override
public int compareTo(Object o) {
return this.age - ((Student)o).getAge();
}
}
package com.qianfeng.day14;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import com.qianfeng.weekendDay03.Student;
public class TreeSetTest {
public static void main(String[] args) {
Set<Student> set=new TreeSet<>();
Student[] student = new Student[5];
student[0] = new Student("1001","张三",18,"12345@qq.com","15675636271");
student[1] = new Student("1002","李四",19,"54321@qq.com","18366543271");
student[2] = new Student("1003","王五",28,"98765@qq.com","12365736271");
student[3] = new Student("1004","赵六",20,"13534@qq.com","19887532271");
student[4] = new Student("1005","田七",22,"56789@qq.com","16765736271");
for(int i = 0; i < 5; i++) {
set.add(student[i]);
}
System.out.println(set);
//结果按年龄从小到大排列输出
}
}
Comparator接口
此接口一般使用int compare(T o1, T o2);方法。
这个方法可以个给两个对象排序。返回值类型为int,它返回负数,0,正数来表明已经存在的对象小于,等于,大于输入对象。
此方法一般写在接口的实现类中,一般用于集合中的sort方法传入。或者用于TreeSet或TreeMap集合的构造器中。例子如下:
package com.qianfeng.day14;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import com.qianfeng.weekendDay03.Student;
public class TreeSetTest {
public static void main(String[] args) {
//采用匿名内部类的方式或者用lambda表达式也行
Set<Student> set=new TreeSet<>(new Comparator<Student>() {
public int compare(Student o1, Student o2) {
return o1.getAge()-o2.getAge();
}
});
//Set<Student> set=new TreeSet<>((s1,s2)->s1.getAge()-s2.getAge());
Student[] student = new Student[5];
student[0] = new Student("1001","张三",18,"12345@qq.com","15675636271");
student[1] = new Student("1002","李四",19,"54321@qq.com","18366543271");
student[2] = new Student("1003","王五",28,"98765@qq.com","12365736271");
student[3] = new Student("1004","赵六",20,"13534@qq.com","19887532271");
student[4] = new Student("1005","田七",22,"56789@qq.com","16765736271");
for(int i = 0; i < 5; i++) {
set.add(student[i]);
}
System.out.println(set);
}
}
两接口之间的相同点
都是用于集合元素间的比较,排序操作
本文详细介绍了Java中的Comparable和Comparator接口在集合排序中的应用。Comparable接口通过compareTo()方法实现对象的自然排序,而Comparator接口通过compare()方法进行定制排序。示例展示了如何在Student类中实现Comparable接口进行年龄排序,以及在TreeSet中使用Comparator接口创建自定义比较器。这两个接口都用于集合元素的比较和排序操作。
622

被折叠的 条评论
为什么被折叠?



