当前位置: 移动技术网 > IT编程>开发语言>Java > Java对List进行排序的两种实现方法

Java对List进行排序的两种实现方法

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

前言

java.util包中的list接口继承了collection接口,用来存放对象集合,所以对这些对象进行排序的时候,要么让对象类自己实现同类对象的比较,要么借助比较器进行比较排序。

学生实体类,包含姓名和年龄属性,比较时先按姓名升序排序,如果姓名相同则按年龄升序排序。

第一种:实体类自己实现比较

(实现comparable接口:public interface comparable<t> ,里面就一个方法声明:public int compareto(t o);

示例代码:

public class student implements comparable<student>{ 
 
 private string name; 
 private int age; 
 public student() { 
  super(); 
  // todo auto-generated constructor stub 
 } 
 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; 
 } 
 @override 
 public int compareto(student o) { 
  // todo auto-generated method stub 
  int flag = this.name.compareto(o.name); 
  if(flag == 0) { 
   flag = this.age - o.age; 
  } 
  return flag; 
 }  
} 

然后利用list类的sort(comparator<? super e> c)方法或java.util.collections工具类的sort(list<t> list) (其实里面就一句:list.sort(null); )进行排序:

list<student> students = new arraylist<student>(); 
students.add(new student("a",10)); 
students.add(new student("b",12)); 
students.add(new student("b",11)); 
students.add(new student("ac",20)); 
students.sort(null); 
//collections.sort(students); 

结果:

  a 10
  ac 20
  b 11
  b 12

第二种:借助比较器进行排序。

示例代码:

 public class student { 
 
 private string name; 
 private int age; 
 public student() { 
  super(); 
  // todo auto-generated constructor stub 
 } 
 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; 
 } 
  
} 

比较器java.util.comparator类是一个接口(public interface comparator<t> ),包含int compare(t o1, t o2);等方法:

我们的比较器要实现该接口并实现compare方法:

private class studentcomparator implements comparator<student> { 
 
 @override 
 public int compare(student o1, student o2) { 
  // todo auto-generated method stub 
  int flag = o1.getname().compareto(o2.getname()); 
  if(flag == 0) { 
   flag = o1.getage() - o2.getage(); 
  } 
  return flag; 
 } 
  
} 

比较的时候可以利用list的sort(comparator<? super e> c)方法(或者java.util.collections工具类的sort(list<t> list, comparator<? super t> c)方法)进行排序。

list<student> students = new arraylist<student>(); 
students.add(new student("a",10)); 
students.add(new student("b",12)); 
students.add(new student("b",11)); 
students.add(new student("ac",20)); 
test t = new test(); 
students.sort(t.new studentcomparator()); 
//collections.sort(students, t.new studentcomparator()); 
for(student student : students) { 
 system.out.println(student.getname()+" "+student.getage()); 
} 

结果跟第一种方法一样:

  a 10
  ac 20
  b 11
  b 12

总结

以上就是关于java中对list进行排序的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

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

相关文章:

验证码:
移动技术网