排序器[Ordering]是Guava流畅风格比较器[Comparator]的实现,它可以用来为构建复杂的比较器,以完成集合排序的功能。从实现上说,Ordering实例就是一个特殊的Comparator实例。Ordering把很多基于Comparator的静态方法(如Collections.max)包装为自己的实例方法(非静态方法),并且提供了链式调用方法,来定制和增强现有的比较器。
创建排序器:常见的排序器可以由下面的静态方法创建
- natural() 对可排序类型做自然排序,如数字按大小,日期按先后排序
- usingToString() 按对象的字符串形式做字典排序[lexicographical ordering]
- from(Comparator) 把给定的Comparator转化为排序器
链式调用方法:通过链式调用,可以由给定的排序器衍生出其它排序器
- reverse() 获取语义相反的排序器
- nullsFirst() 使用当前排序器,但额外把null值排到最前面。
- nullsLast() 使用当前排序器,但额外把null值排到最后面。
- compound(Comparator) 合成另一个比较器,以处理当前排序器中的相等情况。
- lexicographical() 基于处理类型T的排序器,返回该类型的可迭代对象Iterable的排序器。
- onResultOf(Function) 对集合中元素调用Function,再按返回值用当前排序器排序。
package com.cyq.test;
import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;
/**
* @author bh
*/
public class Test1 {
class Foo {
String sortedBy;
int notSortedBy;
public String getSortedBy() {
return sortedBy;
}
public void setSortedBy(String sortedBy) {
this.sortedBy = sortedBy;
}
public int getNotSortedBy() {
return notSortedBy;
}
public void setNotSortedBy(int notSortedBy) {
this.notSortedBy = notSortedBy;
}
public Foo(String sortedBy, int notSortedBy) {
super();
this.sortedBy = sortedBy;
this.notSortedBy = notSortedBy;
}
public Foo() {
super();
}
}
public Test1() {
List<Foo> foos=new ArrayList<>();
foos.add(new Foo("1",1));
foos.add(new Foo("3",3));
foos.add(new Foo("2",2));
foos.add(new Foo(null,4));
Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(
new Function<Foo, String>() {
public String apply(Foo foo) {
return foo.sortedBy;
}
});
List<Foo> foos2 = ordering.sortedCopy(foos);
for(Foo foo:foos2){
System.out.println(foo.getSortedBy());
}
System.out.println("-------------------------------------");
List<Integer> listtest= new ArrayList<>();
listtest.add(1);
listtest.add(2);
listtest.add(1);
listtest.add(4);
Ordering<Integer> naturalIntOrdering = Ordering.natural();
System.out.println("listtest:"+ listtest);
// 判断是否已经排序
System.out.println(naturalIntOrdering.isOrdered(listtest));
// 是否严格有序
System.out.println(naturalIntOrdering.isStrictlyOrdered(listtest));
System.out.println("---------------------------");
List<Integer> listReduce= new ArrayList<>();
for(int i=9;i>0;i--){
listReduce.add(i);
}
System.out.println("naturalIntReduceOrdering:"+ naturalIntOrdering.sortedCopy(listReduce));
System.out.println("listReduce:"+ listReduce);
System.out.println(naturalIntOrdering.isOrdered(naturalIntOrdering.sortedCopy(listReduce)));
System.out.println(naturalIntOrdering.isStrictlyOrdered(naturalIntOrdering.sortedCopy(listReduce)));
System.out.println("---------------------------");
Ordering<String> naturalStringOrdering = Ordering.natural();
List<String> abc = ImmutableList.of("a", "b", "c");
System.out.println(naturalStringOrdering.isOrdered(abc));
System.out.println(naturalStringOrdering.isStrictlyOrdered(abc));
//reverse 获取语义相反的排序器
System.out.println("isOrdered reverse :"+ naturalStringOrdering.reverse().isOrdered(abc));
List<String> cba = ImmutableList.of("c", "b", "a");
System.out.println(naturalStringOrdering.isOrdered(cba));
System.out.println(naturalStringOrdering.isStrictlyOrdered(cba));
System.out.println(cba = naturalStringOrdering.sortedCopy(cba));
System.out.println("leastOf:"+naturalStringOrdering.leastOf(cba, 2));
System.out.println("max:"+naturalStringOrdering.max(cba));
System.out.println("min:"+naturalStringOrdering.min(cba));
System.out.println("-----------------------------");
List<String> list = new ArrayList<>();
list.add("peida");
list.add("jerry");
list.add("harry");
list.add("eva");
list.add("jhon");
list.add("neron");
System.out.println("naturalOrdering:"+ naturalStringOrdering.sortedCopy(list));
System.out.println("leastOf list:"+naturalStringOrdering.leastOf(list, 3));
System.out.println("greatestOf:"+naturalStringOrdering.greatestOf(list, 3));
System.out.println("reverse list :"+ naturalStringOrdering.reverse().sortedCopy(list));
System.out.println("isOrdered list :"+ naturalStringOrdering.isOrdered(list));
System.out.println("isOrdered list :"+ naturalStringOrdering.reverse().isOrdered(list));
list.add(null);
System.out.println(" add null list:"+list);
System.out.println("nullsFirst list :"+ naturalStringOrdering.nullsFirst().sortedCopy(list));
System.out.println("nullsLast list :"+ naturalStringOrdering.nullsLast().sortedCopy(list));
}
public static void main(String[] args) {
new Test1();
}
}