当前位置: 移动技术网 > IT编程>开发语言>Java > java根据List内对象的属性排序方法

java根据List内对象的属性排序方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

方法一:实现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内对象的属性排序方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网