Comparable和Compartor的区别
2018-11-20 03:23:18来源:博客园 阅读 ()
1.List对象实现Comparable接口,使对象具备可比性
package tao; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Student1 implements Comparable<Student1> { private String name; private Integer age; public Student1(String name, Integer age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public int compareTo(Student1 o) { // TODO Auto-generated method stub return this.getAge().compareTo(o.getAge()); } // 测试方法 public static void main(String[] args) { List<Student1> list = new ArrayList<Student1>(); Student1 s1 = new Student1("lisi1",19); Student1 s2 = new Student1("lisi3",17); Student1 s3 = new Student1("lisi2",20); list.add(s1); list.add(s2); list.add(s3); Collections.sort(list); for(Student1 s : list){ System.out.println("name:"+s.getName()+" age:"+s.getAge()); } } }
排序结果 name:lisi3 age:17 name:lisi1 age:19 name:lisi2 age:20
2.List对象不具备可比性,或者对象本身可比性不是想要的排序规则,通过Comparator外部比较
package tao; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Student2 { private String name; private Integer age; public Student2(String name, Integer age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } // 测试方法 public static void main(String[] args) { List<Student2> list = new ArrayList<Student2>(); Student2 s1 = new Student2("lisi1",19); Student2 s2 = new Student2("lisi3",17); Student2 s3 = new Student2("lisi2",20); list.add(s1); list.add(s2); list.add(s3); Collections.sort(list,new MyCompare()); for(Student2 s : list){ System.out.println("name:"+s.getName()+" age:"+s.getAge()); } } } class MyCompare implements Comparator<Object> { @Override public int compare(Object o1, Object o2) { // TODO Auto-generated method stub Student2 s1 = (Student2)o1; Student2 s2 = (Student2)o2; return s1.getAge().compareTo(s2.getAge()); } }
排序结果 name:lisi3 age:17 name:lisi1 age:19 name:lisi2 age:20
3.Comparable和Compartor的区别
a.出处不同
java.util.Comparator;
java.lang.Comparable;
b.提供方法不同
Comparator提供int compare(Object o1, Object o2)
Comparable提供int compareTo(Object o)
c.内外部区别
Comparator提供外部对比
Comparable提供对象自身对比
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- System.out.println()和System.out.print()区别 2020-06-10
- ArrayList是如何实现的,ArrayList和LinedList的区别?Array 2020-05-19
- java基础面试题1 2020-05-19
- throws和throw的区别 2020-05-13
- 面试被问ReentrantLock的公平锁与非公平锁,区别以及实现 2020-05-12
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash