How To Sort Objects With Java Interface Comparable and Comparator
2011-10-20 11:32
531 查看
Java 提供了Comparable 和Comparator 两个接口让程序员可以实现Java对象的排序。
首先总结下Comparable 与Comparator接口的区别:
Comparable 实现对象方式:
Comparator 实现排序方式:
首先总结下Comparable 与Comparator接口的区别:
java.lang.Comparable | java.util.Comparator |
int objOne.compareTo(objTwo) | int compare (objOne, objTwo) |
返回: 负数: 如果 objOne < objTwo 零: 如果 objOne == objTwo 正数: 如果 objOne > objTwo | 同 Comparable |
必须修改要比较的类的结构,令其实现Comparable 接口 | 编写一个独立的类,令其实现Comparator接口 |
一个类只能有一种排序 | 一个类可以有多重排序方式,只要多编写一个Comparator的实现类就好了。 |
JAVA API中经常用到,例如:String, 包装类(Integer, Long, Double…), Date, Calendar … | 用于对第三方类的排序,因为第三方类一般都是无法修改的,更别说实现Comparable了,所以只能使用Comparator了。 |
package com.citi.crc; public class AudiCar implements Comparable<AudiCar> { private String series; private Double price; private Double speed; public String getSeries() { return series; } public void setSeries(String series) { this.series = series; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Double getSpeed() { return speed; } public void setSpeed(Double speed) { this.speed = speed; } /* (non-Javadoc) * @see java.lang.Comparable#compareTo(java.lang.Object) */ @Override public int compareTo(AudiCar o) { return this.getPrice().compareTo(o.getPrice()); } } package com.citi.crc; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class TestAudiCar { public static void main(String[] args) { List<AudiCar> cars = new ArrayList<AudiCar>(); AudiCar audiA3 = new AudiCar(); audiA3.setSeries("A3"); audiA3.setPrice(200000d); audiA3.setSpeed(80d); AudiCar audiA4 = new AudiCar(); audiA4.setSeries("A4"); audiA4.setPrice(260000d); audiA4.setSpeed(90d); AudiCar audiA6 = new AudiCar(); audiA6.setSeries("A6"); audiA6.setPrice(320000d); audiA6.setSpeed(100d); cars.add(audiA4); cars.add(audiA6); cars.add(audiA3); Collections.sort(cars); // Collections.reverse(cars); for (AudiCar car : cars) { System.out.println(car.getPrice()); } } }
Comparator 实现排序方式:
package com.citi.crc; import java.util.Comparator; public class AudiCar { private String series; private Double price; private Double speed; public String getSeries() { return series; } public void setSeries(String series) { this.series = series; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Double getSpeed() { return speed; } public void setSpeed(Double speed) { this.speed = speed; } static class PriceComparator implements Comparator<AudiCar> { /* (non-Javadoc) * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) */ @Override public int compare(AudiCar o1, AudiCar o2) { return o1.getPrice().compareTo(o2.getPrice()); } } static class SpeedComparator implements Comparator<AudiCar> { /* (non-Javadoc) * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) */ @Override public int compare(AudiCar o1, AudiCar o2) { return o1.getSpeed().compareTo(o2.getSpeed()); } } } package com.citi.crc; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class TestAudiCar { public static void main(String[] args) { List<AudiCar> cars = new ArrayList<AudiCar>(); AudiCar audiA3 = new AudiCar(); audiA3.setSeries("A3"); audiA3.setPrice(200000d); audiA3.setSpeed(80d); AudiCar audiA4 = new AudiCar(); audiA4.setSeries("A4"); audiA4.setPrice(260000d); audiA4.setSpeed(90d); AudiCar audiA6 = new AudiCar(); audiA6.setSeries("A6"); audiA6.setPrice(320000d); audiA6.setSpeed(100d); cars.add(audiA4); cars.add(audiA6); cars.add(audiA3); Collections.sort(cars, new AudiCar.PriceComparator()); // Collections.sort(cars, new AudiCar.SpeedComparator()); for (AudiCar car : cars) { System.out.println(car.getPrice()); } } }
相关文章推荐
- How to use Comparator and Comparable in Java? With example
- When to use comparable and comparator interfaces in java
- how to use a SQLite database in a standalone program with an HTML interface and VBScript as the programming language
- How to open and sort a CSV file with Ruby
- How to use UTF-8_with_BOM, XML and Java together
- 着重基础之—Java 8 Comparator: How to Sort a List (List排序)
- How to sort an NSMutableArray with custom objects in it?
- How-To : Using EJB 3.0 and Java Persistence API with Spring in OC4J
- How to Install Apache Tomcat 8 (on Windows, Mac OS X, Ubuntu) and Get Started with Java Servlet Prog
- Create intelligent Web spiders -How to use Java network objects and HTML objects
- How to get started with WebRTC and iOS without wasting 10 hours of your life
- How to use java Properties API -- read and write
- 如何设置Java桌面程序界面外观(How to Set the Look and Feel)
- 如何设置Java桌面程序界面外观(How to Set the Look and Feel)
- The Comparator and Comparable in Java
- (转贴)How to use JDBC and transactions in Tomcat with JOTM
- How to use equals( ) and equalsIgnoreCase( ) in Java
- How to implement the similar mechanism with Java final in c++?
- 关于JAVA界面风格(How to Set the Look and Feel)
- How to install java and eclipse on linux