一、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;
}
}

最新文章

  1. 简单的jQuery幻灯片实现
  2. node.js-概念
  3. IOS: 模型面数控制
  4. C#使用ajaxForm进行上传图片
  5. jenkins2 pipeline高级
  6. iOS边练边学--文件压缩和解压缩的第三方框架SSZipArchive的简单使用
  7. Map写入的顺序 输出地顺序ZT
  8. opencv 实现进度控制
  9. Keil C -WARNING L15: MULTIPLE CALL TO SEGMENT
  10. android stagefright基本流程总结
  11. 项目管理实践【五】自动编译和发布网站【Using Visual Studio with Source Control System to build and publish website automatically】
  12. C 其他一些
  13. yii2 源码分析 Component类分析 (二)
  14. c#格林治时间实现
  15. 在Windows Server 2008 R2上安装IIS服务
  16. Wed 基础
  17. code自动补全
  18. 【springmvc】之使用jQuery接收前端传入List对象
  19. C++ Custom Control控件 向父窗体发送对应的消息
  20. 带你开始进入NPM的世界之NPM包的开发

热门文章

  1. atm-interface-shopping
  2. ThinkPHP邮件发送S(Smtp + Mail + phpmailer)
  3. 从源码带你看懂functools的partial方法
  4. MapReduce实现单词统计
  5. IOS开发学习笔记028-UITableView单组数据显示代码优化
  6. php代码审计 strcmp和MD5函数漏洞
  7. python学习-- 在django中,执行原始sql语句
  8. 【LeetCode】汉明距离(Hamming Distance)
  9. 关于caffe 是如何卷积的一点总结
  10. Java中转发与重定向的区别