集合排序 Comparator和Comparable的使用区别

Java 排序 Compare 

在Java中使用集合来存储数据时非常常见的,集合排序功能也是常用功能之一.下面看一下如何进行集合排序,常用的方法有: ComparatorComparable

Comparator接口

使用步骤:

  1. 新建比较类,
  2. 实现Comparator接口,
  3. 重写compare方法,
  1. package sort; 


  2. import java.util.Comparator; 


  3. public class LuckBoyCompare implements Comparator<LuckBoy>{ 


  4. @Override 

  5. public int compare(LuckBoy o1, LuckBoy o2) { 

  6. return o1.getAge()-o2.getAge(); 







  • 调用Collections.sort()方法进行排序,
  • 形式:Collections.sort(集合, 比较器实例).
  1. @Test 

  2. public void test1() { 

  3. List<LuckBoy> boyList = new ArrayList<LuckBoy>(); 

  4. LuckBoy boy1 = new LuckBoy("张三",13,"上海"); 

  5. LuckBoy boy2 = new LuckBoy("李四",12,"北京"); 

  6. LuckBoy boy3 = new LuckBoy("王五",18,"深圳"); 

  7. LuckBoy boy4 = new LuckBoy("马六",17,"南京"); 


  8. boyList.add(boy1); 

  9. boyList.add(boy2); 

  10. boyList.add(boy3); 

  11. boyList.add(boy4); 


  12. System.out.println("排序前:"); 

  13. for (LuckBoy luckBoy : boyList) { 

  14. System.out.println(luckBoy); 




  15. System.out.println("排序后:"); 

  16. Collections.sort(boyList, new LuckBoyCompare()); 

  17. for (LuckBoy luckBoy : boyList) { 

  18. System.out.println(luckBoy); 





LuckBoy.java

  1. package sort; 


  2. public class LuckBoy{ 

  3. private String name; 

  4. private Integer age; 

  5. private String city; 


  6. public LuckBoy() { 

  7. super(); 




  8. public LuckBoy(String name, Integer age, String city) { 

  9. super(); 

  10. this.name = name; 

  11. this.age = age; 

  12. this.city = city; 




  13. public String getName() { 

  14. return name; 



  15. public void setName(String name) { 

  16. this.name = name; 



  17. public Integer getAge() { 

  18. return age; 



  19. public void setAge(Integer age) { 

  20. this.age = age; 



  21. public String getCity() { 

  22. return city; 



  23. public void setCity(String city) { 

  24. this.city = city; 




  25. @Override 

  26. public String toString() { 

  27. return "LuckBoy [name=" + name + ", age=" + age + ", city=" + city + "]"; 





打印结果:

排序前:
LuckBoy [name=张三, age=13, city=上海]
LuckBoy [name=李四, age=12, city=北京]
LuckBoy [name=王五, age=18, city=深圳]
LuckBoy [name=马六, age=17, city=南京]
排序后:
LuckBoy [name=李四, age=12, city=北京]
LuckBoy [name=张三, age=13, city=上海]
LuckBoy [name=马六, age=17, city=南京]
LuckBoy [name=王五, age=18, city=深圳]

Comparable接口

使用步骤:

  • 数据模型实现Comparable接口,
  • 重写compareTo方法,
  1. package sort; 


  2. public class LuckBoy implements Comparable<LuckBoy>{ 

  3. //TODO 中间代码省略 

  4. @Override 

  5. public int compareTo(LuckBoy o) { 

  6. return this.age-o.age; 






  • 调用Collections.sort()方法进行排序,
  • 形式:Collections.sort(集合)
  1. @Test 

  2. public void test2() { 

  3. List<LuckBoy> boyList = new ArrayList<LuckBoy>(); 

  4. LuckBoy boy1 = new LuckBoy("张三",13,"上海"); 

  5. LuckBoy boy2 = new LuckBoy("李四",12,"北京"); 

  6. LuckBoy boy3 = new LuckBoy("王五",18,"深圳"); 

  7. LuckBoy boy4 = new LuckBoy("马六",17,"南京"); 


  8. boyList.add(boy1); 

  9. boyList.add(boy2); 

  10. boyList.add(boy3); 


  11. boyList.add(boy4); 


  12. System.out.println("============================"); 

  13. System.out.println("排序前:"); 

  14. for (LuckBoy luckBoy : boyList) { 

  15. System.out.println(luckBoy); 




  16. System.out.println("排序后:"); 

  17. Collections.sort(boyList); 

  18. for (LuckBoy luckBoy : boyList) { 

  19. System.out.println(luckBoy); 





打印结果:

排序前:
LuckBoy [name=张三, age=13, city=上海]
LuckBoy [name=李四, age=12, city=北京]
LuckBoy [name=王五, age=18, city=深圳]
LuckBoy [name=马六, age=17, city=南京]
排序后:
LuckBoy [name=李四, age=12, city=北京]
LuckBoy [name=张三, age=13, city=上海]
LuckBoy [name=马六, age=17, city=南京]
LuckBoy [name=王五, age=18, city=深圳]

区别

Comparator 使用灵活,不需要修改源码.但是,使用时需要传入比较器对象;

Comparable 使用简单,但是需要修改源码.

上一篇:Java://Comparator、Comparable的用法(按照要求将set集合的数据进行排序输出):


下一篇:基于现有数据库的Code First模式迁移更新数据库