java根据List内对象的属性排序方法
2018-01-03 14:36
716 查看
方法一:实现Comparator接口,并重写compare方法
实体类代码:
import java.util.Comparator; /** * 学生类 方法一 * 实现Comparator接口 * 并重写compare方法 * @author liaot * */ public class Student implements Comparator<Student>{ private String name; //姓名 private int age; //年龄 //重写 比较方法 本次例子定义为按年龄比较 @Override public int compare(Student o1, Student o2) { if(o1.getAge() > o2.getAge()){ return 1; }else{ return -1; } } public Student(String name, int age) { super(); this.name = name; this.age = age; } 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; } }
测试类:
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] args) { //初始化四个不同的学生 Student stu1 = new Student("路人甲", 20); Student stu2 = new Student("路人已", 18); Student stu3 = new Student("路人丙", 16); Student stu4 = new Student("路人丁", 19); //新建List把学生加进List List<Student> stuList = new ArrayList<>(); stuList.add(stu1); stuList.add(stu2); stuList.add(stu3); stuList.add(stu4); System.out.println("排序前:====="); for(Student stu :stuList){ System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge()); } //排序 Collections.sort(stuList, stu1); //第一个参数为List 第二个参数为对象的一个实例 System.out.println("排序后:====="); for(Student stu :stuList){ System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge()); } } }
运行结果:
方法二:实现Comparable接口 并重写compareTo方法
/** * 学生类 方法二 实现Comparable接口 并重写compareTo方法 * * @author liaot * */ public class Student2 implements Comparable<Student2> { private String name; // 姓名 private int age; // 年龄 // 重写 比较方法 本次例子定义为按年龄比较 @Override public int compareTo(Student2 stu) { if (this.age > stu.getAge()) { return 1; } else { return -1; } } public Student2(String name, int age) { super(); this.name = name; this.age = age; } 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; } }
测试类
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main2 { public static void main(String[] args) { //初始化四个不同的学生 Student2 stu1 = new Student2("路人甲", 20); Student2 stu2 = new Student2("路人已", 18); Student2 stu3 = new Student2("路人丙", 16); Student2 stu4 = new Student2("路人丁", 19); //新建List把学生加进List List<Student2> stuList = new ArrayList<>(); stuList.add(stu1); stuList.add(stu2); stuList.add(stu3); stuList.add(stu4); System.out.println("排序前:====="); for(Student2 stu :stuList){ System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge()); } //排序 Collections.sort(stuList); //只有一个参数参数为List System.out.println("排序后:====="); for(Student2 stu :stuList){ System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge()); } } }
运行结果
三、总结:两种方式写法和用法上的区别:
以上这篇java根据List内对象的属性排序方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- Java:集合,对列表(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法
- java根据List内对象的属性排序
- Java(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法
- 根据List的对象的某个属性进行排序方法
- Java面试题,List集合中根据对象的某一属性排序
- JAVA中根据集合中对象属性排序的方法
- 用Collections.sort方法对list排序有两种方法 第一种是list中的对象实现Comparable接口,如下: [java] view plain copy /** * 根据ord
- Java实现根据List中对象的相应属性进行排序
- java使用Comparator根据list对象的属性进行排序
- java list 根据对象的一个属性排序
- Java中对List中的存储对象根据属性进行排序的办法
- 【原创】Java 对 List 内对象根据对象属性进行排序
- list集合根据对象属性排序举例
- 对象集合list根据对象属性排序2015-8-16
- List排序之根据对象属性
- java中list按照某个属性排序方法
- 花旗软件 电面问题(一) 根据对象的某一属性排序一组对象(JAVA实现)
- List集合中的对象根据属性排序
- 如何根据对象的属性,对集合(list / set)中的对象进行排序
- Java实现根据属性排序输出对象