Java中Collections的sort方法和Comparable与Comparator的比较
2024-08-28 18:50:09
一、Comparable
新建Student1类,类实现Comparable接口,并重写compareTo方法
public class Student1 implements Comparable<Student1> { private String name;
private int age; public Student1() {
} public Student1(String name, int age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public int compareTo(Student1 s) {
int num = this.age - s.age;
int num1 = (num == 0 ? this.name.compareTo(s.name) : num);
return num1;
}
}
调用
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; public class Student1Test {
public static void main(String[] args) {
List<Student1> list1 = new ArrayList<Student1>();
list1.add(new Student1("林青霞", 27));
list1.add(new Student1("风清扬", 30));
list1.add(new Student1("刘晓曲", 28));
list1.add(new Student1("武鑫", 29));
list1.add(new Student1("林青霞", 27)); Collections.sort(list1);
for (Student1 s : list1) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
二、Comparator
新建Student2类
public class Student2 { private String name;
private int age; public Student2() {
} public Student2(String name, int age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}
调用
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class Student2Test {
public static void main(String[] args) {
List<Student2> list2 = new ArrayList<Student2>();
list2.add(new Student2("林青霞", 27));
list2.add(new Student2("风清扬", 30));
list2.add(new Student2("刘晓曲", 28));
list2.add(new Student2("武鑫", 29));
list2.add(new Student2("林青霞", 27));
//方式一
Collections.sort(list2, new MyComparator());
for (Student2 s : list2) {
System.out.println(s.getName() + "---" + s.getAge());
}
//逆序
//方式二:匿名类
Collections.sort(list2, new Comparator<Student2>() {
@Override
public int compare(Student2 s1, Student2 s2) {
int num = s2.getAge() - s1.getAge();
int num1 = (num == 0 ? s2.getName().compareTo(s1.getName())
: num);
return num1;
}
});
for (Student2 s : list2) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
} class MyComparator implements Comparator<Student2> {
@Override
public int compare(Student2 s1, Student2 s2) {
int num = s1.getAge() - s2.getAge();
int num1 = (num == 0 ? s1.getName().compareTo(s2.getName()) : num);
return num1;
}
}
最新文章
- 简单的jQuery幻灯片实现
- node.js-概念
- IOS: 模型面数控制
- C#使用ajaxForm进行上传图片
- jenkins2 pipeline高级
- iOS边练边学--文件压缩和解压缩的第三方框架SSZipArchive的简单使用
- Map写入的顺序 输出地顺序ZT
- opencv 实现进度控制
- Keil C -WARNING L15: MULTIPLE CALL TO SEGMENT
- android stagefright基本流程总结
- 项目管理实践【五】自动编译和发布网站【Using Visual Studio with Source Control System to build and publish website automatically】
- C 其他一些
- yii2 源码分析 Component类分析 (二)
- c#格林治时间实现
- 在Windows Server 2008 R2上安装IIS服务
- Wed 基础
- code自动补全
- 【springmvc】之使用jQuery接收前端传入List对象
- C++ Custom Control控件 向父窗体发送对应的消息
- 带你开始进入NPM的世界之NPM包的开发