转载请标明出处:http://blog.csdn.net/xx326664162/article/details/52227690 文章出自:薛瑄的博客
你也可以查看我的其他同类文章,也会让你有一定的收货
第一种:Comparable 排序接口
若一个类实现了Comparable接口,就意味着“该类支持排序”。 假设“有一个List列表(或数组),里面的元素是实现了Comparable接口的类”,则该List列表(或数组)可以通过 Collections.sort(或 Arrays.sort)进行排序。
此外,“实现Comparable接口的类的对象”可以用作“有序映射(如TreeMap)”中的键或“有序集合(TreeSet)”中的元素,而不需要指定比较器。
class A implements Comparable<A>{
private String name;
private Integer order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
@Override
public String toString() {
return "name is "+name+" order is "+order;
}
@Override
public int compareTo(A a) {
return this.order.compareTo(a.getOrder());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
List< A> list = new ArrayList< A>();
- 类A实现接口Comparable,并实现compareTo()方法
- 调用Collections.sort(lists)即可实现排序
第二种:Comparator比较器接口。
我们若需要控制某个类的次序,而该类本身不支持排序(即没有实现Comparable接口);我们可以建立一个“比较器”来进行排序。这个“比较器”只需要实现Comparator接口即可。
-
Collections.sort(list, new PriceComparator())
- 参数一:需要排序的list
- 参数二:比较器,实现Comparator接口的类,返回一个int型的值,就相当于一个标志,告诉sort方法按什么顺序来对list进行排序。
-
Comparator是个接口,可重写compare()及equals()这两个方法,用于比较功能;如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f,g这样,当然数字也是这样的。
- compare(a,b)方法:根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数。
- equals(obj)方法:仅当指定的对象也是一个 Comparator,并且强行实施与此 Comparator 相同的排序时才返回 true。
两种方法示例
package com.jabberchina.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortTest {
static class A implements Comparable<A> {
private String name;
private Integer order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
@Override
public String toString() {
return "name is " + name + " order is " + order;
}
@Override
public int compareTo(A a) {
return this.order.compareTo(a.getOrder());
}
}
static class B {
private String name;
private String order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
@Override
public String toString() {
return "name is " + name + " order is " + order;
}
}
public static void main(String[] args) {
List<String> lists = new ArrayList<String>();
lists.add("5");
lists.add("2");
lists.add("9");
Collections.sort(lists);
List<A> listA = new ArrayList<A>();
A a1 = new A();
a1.setName("a1");
a1.setOrder(1);
A a2 = new A();
a2.setName("a2");
a2.setOrder(2);
listA.add(a1);
listA.add(a2);
Collections.sort(listA);
List<B> listB = new ArrayList<B>();
B b1 = new B();
b1.setName("b1");
b1.setOrder("a");
B b2 = new B();
b2.setName("b2");
b2.setOrder("b");
listB.add(b1);
listB.add(b2);
Collections.sort(listB, new Comparator<B>() {
@Override
public int compare(B b1, B b2) {
return b1.getOrder().compareTo(b2.getOrder());
}
});
System.out.println(lists);
System.out.println(listA);
System.out.println(listB);
}
}
打印的结果为:
[2, 5, 9]
[name is a1 order is 1, name is a2 order is 2]
[name is b1 order is a, name is b2 order is b]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
再例举一个方法一的示例:
方法二是一个策略模式,可以灵活替换比较器,实现不同排序
Book实体类:
public class Book implements Comparable {
public int id;
public String name;
public double price;
private String author;
public GregorianCalendar calendar;
public Book() {
this(0, "X", 0.0, new GregorianCalendar(), "");
}
public Book(int id, String name, double price, GregorianCalendar calender,
String author) {
this.id = id;
this.name = name;
this.price = price;
this.calendar = calender;
this.author = author;
}
public String toString() {
String showStr = id + "\t" + name;
DecimalFormat formatPrice = new DecimalFormat("0.00");
showStr += "\t" + formatPrice.format(price);
showStr += "\t" + author;
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy年MM月dd日");
showStr += "\t" + formatDate.format(calendar.getTime());
return showStr;
}
public int compareTo(Object obj) {
Book b = (Book) obj;
return this.id - b.id;
}
public static void main(String[] args) {
Book b1 = new Book(10000, "红楼梦", 150.86, new GregorianCalendar(2009,
01, 25), "曹雪芹、高鄂");
Book b2 = new Book(10001, "三国演义", 99.68, new GregorianCalendar(2008, 7,
8), "罗贯中 ");
Book b3 = new Book(10002, "水浒传", 100.8, new GregorianCalendar(2009, 6,
28), "施耐庵 ");
Book b4 = new Book(10003, "西游记", 120.8, new GregorianCalendar(2011, 6,
8), "吴承恩");
Book b5 = new Book(10004, "天龙八部", 10.4, new GregorianCalendar(2011, 9,
23), "搜狐");
TreeMap tm = new TreeMap();
tm.put(b1, new Integer(255));
tm.put(b2, new Integer(122));
tm.put(b3, new Integer(688));
tm.put(b4, new Integer(453));
tm.put(b5, new Integer(40));
Iterator it = tm.keySet().iterator();
Object key = null, value = null;
Book bb = null;
while (it.hasNext()) {
key = it.next();
bb = (Book) key;
value = tm.get(key);
System.out.println(bb.toString() + "\t库存:" + tm.get(key));
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
自定义比较器和测试类:
package com.tjcyjd.comparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
public class UseComparator {
public static void main(String args[]) {
List<Book> list = new ArrayList<Book>();
Book b1 = new Book(10000, "红楼梦", 150.86, new GregorianCalendar(2009,
01, 25), "曹雪芹、高鄂");
Book b2 = new Book(10001, "三国演义", 99.68, new GregorianCalendar(2008, 7,
8), "罗贯中 ");
Book b3 = new Book(10002, "水浒传", 100.8, new GregorianCalendar(2009, 6,
28), "施耐庵 ");
Book b4 = new Book(10003, "西游记", 120.8, new GregorianCalendar(2011, 6,
8), "吴承恩");
Book b5 = new Book(10004, "天龙八部", 10.4, new GregorianCalendar(2011, 9,
23), "搜狐");
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
list.add(b5);
System.out.println("数组序列中的元素:");
myprint(list);
Collections.sort(list, new PriceComparator());
System.out.println("按书的价格排序:");
myprint(list);
Collections.sort(list, new CalendarComparator());
System.out.println("按书的出版时间排序:");
myprint(list);
}
static class PriceComparator implements Comparator {
public int compare(Object object1, Object object2) {
Book p1 = (Book) object1;
Book p2 = (Book) object2;
return new Double(p1.price).compareTo(new Double(p2.price));
}
}
static class CalendarComparator implements Comparator {
public int compare(Object object1, Object object2) {
Book p1 = (Book) object1;
Book p2 = (Book) object2;
return p2.calendar.compareTo(p1.calendar);
}
}
public static void myprint(List<Book> list) {
Iterator it = list.iterator();
while (it.hasNext()) {
System.out.println("\t" + it.next());
}
}
}