java_Comparable & Comparator

本文介绍了Java中两个重要的排序接口:Comparable和Comparator。通过实例演示了如何使用这两个接口进行排序,包括自定义排序逻辑。此外还介绍了TreeMap及其自定义排序方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java有2个非常重要的排序接口:java.lang.Comparable和java.util.Comparator,
前者是基础包下的,主要通过继承该接口实现类的排序功能,工具包下的主要通过非继承的方式实现排序。


package sunfa;

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

public class ComparableDemo1 {
public static void main(String[] args) {
/**
* 一个类实现了java.lang.Comparable接口可以实现排序。
* 或者一个类利用java.util.Comparator接口实现排序
*/
Person per[] = { new Person("张三", 10, 75), new Person("李四", 11, 85),
new Person("王五", 9, 75), new Person("陈留", 13, 75) };
//先按souce由小到大,再由age由大到小的顺序排列
Arrays.sort(per);
// Arrays.sort(per, new Comparator<Person>() {
// public int compare(Person o1, Person o2) {
// if (o1.getSouce() > o2.getSouce()) {
// return 1;
// } else if (o1.getSouce() < o2.getSouce()) {
// return -1;
// } else {
// if (o1.getAge() > o2.getAge()) {
// return -1;
// } else if (o1.getAge() < o2.getAge()) {
// return 1;
// } else {
// return 0;
// }
// }
// }
// });
System.out.println("name\tage\tsouce");
for (int i = 0; i < per.length; i++) {
System.out.println(per[i]);
}
}
}

class Person implements Comparable<Person>{
private String name;
private int age;
private float souce;

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 getSouce() {
return souce;
}

public void setSouce(float souce) {
this.souce = souce;
}

public Person(String name, int age, float souce) {
super();
this.name = name;
this.age = age;
this.souce = souce;
}

public String toString() {
return this.name + "\t" + this.age + "\t" + this.souce;
}

public int compareTo(Person o) {
if (this.souce > o.souce) {
return 1;
} else if (this.souce < o.souce) {
return -1;
} else {
if (this.age > o.age) {
return -1;
} else if (this.age < o.age) {
return 1;
} else {
return 0;
}
}
}
}



java.util.TreeMap是红黑树的实现版本,并且支持自定义排序

public TreeMap(Comparator<? super K> c) {
this.comparator = c;
}



java.util.Arrays.stors(...)和TreeMap分别是利用二分法和平衡二叉树来实现对象排序功能的。


package sunfa;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeMap;

class Person1 implements Comparator<Person1> {
private String name;
private int age;
private float souce;

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 getSouce() {
return souce;
}

public void setSouce(float souce) {
this.souce = souce;
}

public Person1() {
super();
}

public Person1(String name, int age, float souce) {
super();
this.name = name;
this.age = age;
this.souce = souce;
}

public String toString() {
return this.name + "\t" + this.age + "\t" + this.souce;
}

public int compareTo(Person1 o) {
return 0;
}

public int compare(Person1 o1, Person1 o2) {
if (o1.souce > o2.souce) {
return 1;
} else if (o1.souce < o2.souce) {
return -1;
} else {
if (o1.age > o2.age) {
return -1;
} else if (o1.age < o2.age) {
return 1;
} else {
return 0;
}
}
}
}
public class TreeMapTest {
public static void main(String[] args) {
TreeMap<Person1, Person1> tree = new TreeMap<Person1, Person1>(
new Person1());
Person1 per[] = { new Person1("张三", 10, 75), new Person1("李四", 11, 85),
new Person1("王五", 9, 75), new Person1("陈留", 13, 75) };
for (int i = 0; i < per.length; i++) {
tree.put(per[i], null);
}
Iterator<Person1> it = tree.keySet().iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值