1. 程式人生 > >Google-Guava學習:排序器Ordering

Google-Guava學習:排序器Ordering

排序器[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(); } }