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

 

  • Comparator接口
  • Comparable接口
  • 区别

 

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

Comparator接口

使用步骤:

  1. 新建比较类,
  2. 实现Comparator接口,
  3. 重写compare方法,
  1. package sort; 
  2.  
  3. import java.util.Comparator; 
  4.  
  5. public class LuckBoyCompare implements Comparator<LuckBoy>
  6.  
  7. @Override 
  8. public int compare(LuckBoy o1, LuckBoy o2)
  9. return o1.getAge()-o2.getAge(); 

  10.  

  11.  
  • 调用Collections.sort()方法进行排序,
  • 形式:Collections.sort(集合, 比较器实例).
  1. @Test 
  2. public void test1()
  3. List boyList = new ArrayList(); 
  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.  
  9. boyList.add(boy1); 
  10. boyList.add(boy2); 
  11. boyList.add(boy3); 
  12. boyList.add(boy4); 
  13.  
  14. System.out.println("排序前:"); 
  15. for (LuckBoy luckBoy : boyList) { 
  16. System.out.println(luckBoy); 

  17.  
  18. System.out.println("排序后:"); 
  19. Collections.sort(boyList, new LuckBoyCompare()); 
  20. for (LuckBoy luckBoy : boyList) { 
  21. System.out.println(luckBoy); 


LuckBoy.java

  1. package sort; 
  2.  
  3. public class LuckBoy
  4. private String name; 
  5. private Integer age; 
  6. private String city; 
  7.  
  8. public LuckBoy()
  9. super(); 

  10.  
  11. public LuckBoy(String name, Integer age, String city)
  12. super(); 
  13. this.name = name; 
  14. this.age = age; 
  15. this.city = city; 

  16.  
  17. public String getName()
  18. return name; 

  19. public void setName(String name)
  20. this.name = name; 

  21. public Integer getAge()
  22. return age; 

  23. public void setAge(Integer age)
  24. this.age = age; 

  25. public String getCity()
  26. return city; 

  27. public void setCity(String city)
  28. this.city = city; 

  29.  
  30. @Override 
  31. public String toString()
  32. 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.  
  3. public class LuckBoy implements Comparable<LuckBoy>
  4. //TODO 中间代码省略 
  5. @Override 
  6. public int compareTo(LuckBoy o)
  7. return this.age-o.age; 


  8.  
  • 调用Collections.sort()方法进行排序,
  • 形式:Collections.sort(集合)
  1. @Test 
  2. public void test2()
  3. List boyList = new ArrayList(); 
  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.  
  9. boyList.add(boy1); 
  10. boyList.add(boy2); 
  11. boyList.add(boy3); 
  12.  
  13. boyList.add(boy4); 
  14.  
  15. System.out.println("============================"); 
  16. System.out.println("排序前:"); 
  17. for (LuckBoy luckBoy : boyList) { 
  18. System.out.println(luckBoy); 

  19.  
  20. System.out.println("排序后:"); 
  21. Collections.sort(boyList); 
  22. for (LuckBoy luckBoy : boyList) { 
  23. 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 使用简单,但是需要修改源码.

转载于:https://www.cnblogs.com/linyufeng/p/8465170.html

你可能感兴趣的:(集合排序 Comparator和Comparable的使用区别)