Guava学习笔记(4)--Ordering简介[翻译+学习笔记]

Ordering是Guava最常用的Comparator类, 可以用来操作, 扩展和使用comparators.

自定义比较器, 进行排序判断, 进行排序, 获取最大最小值, 获取最大最小的前几个值等方法.

几个常用的static方法, 提供了三种常用的比较器

方法 描述
natural() 使用Comparable类型的自然顺序, 例如, 整数从小到大, 字符串是按字典顺序.
usingToString() 使用toString()返回的字符串按字典顺序进行排序
arbitrary() 返回一个所有对象的任意顺序, 即compare(a, b) == 0 就是 a == b (identity equality). 本身的排序是没有任何含义, 但是在VM的生命周期是一个常量.

自定义Ordering

 1 import java.util.Arrays;
 2 import java.util.List;
 3 
 4 import com.google.common.collect.Ordering;
 5 import com.google.common.primitives.Ints;
 6 
 7 public class Demo {
 8     public static void main(String[] args) {
 9         Ordering<String> byLengthOrdering = new Ordering<String>() {
10             public int compare(String left, String right) {
11                 return Ints.compare(left.length(), right.length());
12             }
13         };
14         List<String> strList = Arrays.asList("abc", "a", "bcd");
15         System.out.println(byLengthOrdering.reverse().isOrdered(strList));
16         
17         List<String> strList1 = Arrays.asList("a", "ab", "bcd");
18         System.out.println(byLengthOrdering.isOrdered(strList1));
19     }
20 }

操作方法

reverse()方法, 获取了Ordering的反排序. 使用自定义的Ordering判断collection是否符合自定义顺序.

方法 描述
reverse() 返回与当前Ordering相反的排序.
nullsFirst() 返回一个将null放在non-null元素之前的Ordering, 其他的和原始的Ordering一样. 又见nullsLast().
compound(Comparator) 返回一个使用Comparator的Ordering, Comparator作为第二排序元素, 例如对bug列表进行排序, 先根据bug的级别, 再根据优先级进行排序.
lexicographical() Returns an Ordering that orders iterables lexicographically by their elements.
onResultOf(Function) 将function应用在各个元素上之后, 在使用原始ordering进行排序.

下面是使用

class Foo {
  @Nullable String sortedBy;
  int notSortedBy;
}

Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(new Function<Foo, String>() {
    public String apply(Foo foo) {
      return foo.sortedBy;
    }
  });

还有一些很有用的方法

Method Description See also
greatestOf(Iterable iterable, int k) Returns the k greatest elements of the specified iterable, according to this ordering, in order from greatest to least. Not necessarily stable. leastOf
isOrdered(Iterable) Tests if the specified Iterable is in nondecreasing order according to this ordering. isStrictlyOrdered
sortedCopy(Iterable) Returns a sorted copy of the specified elements as a List. immutableSortedCopy
min(E, E) Returns the minimum of its two arguments according to this ordering. If the values compare as equal, the first argument is returned. max(E, E)
min(E, E, E, E...) Returns the minimum of its arguments according to this ordering. If there are multiple least values, the first is returned. max(E, E, E, E...)
min(Iterable) Returns the minimum element of the specified Iterable. Throws a NoSuchElementException if the Iterable is empty. max(Iterable), min(Iterator), max(Iterator)

最后以一个例子结尾吧, 都很简单, 只是简单的使用api, 这些方法的命名可以看到其用途, 好的命名方法可以见名知义

 1 import java.util.List;
 2 
 3 import com.google.common.collect.ImmutableList;
 4 import com.google.common.collect.Ordering;
 5 
 6 public class NaturalOrderingDemo {
 7     public static void main(String[] args) {
 8         Ordering<String> natural = Ordering.natural();
 9         
10         List<String> abc = ImmutableList.of("a", "b", "c");
11         System.out.println(natural.isOrdered(abc));
12         
13         List<String> cab = ImmutableList.of("c", "a", "b");
14         System.out.println(natural.isOrdered(cab));
15         System.out.println(cab = natural.sortedCopy(cab));
16         System.out.println(natural.max(cab));
17         System.out.println(natural.min(cab));
18         System.out.println(natural.leastOf(cab, 2));
19         System.out.println(natural.greatestOf(cab, 2));
20         
21         System.out.println("====================================");
22         
23         Ordering<Integer> intNatural = Ordering.natural();
24         
25         List<Integer> a123 = ImmutableList.of(1, 2, 3);
26         System.out.println(intNatural.isOrdered(a123));
27         
28         List<Integer> a132 = ImmutableList.of(1, 3, 2);
29         System.out.println(intNatural.isOrdered(a132));
30         System.out.println(intNatural.sortedCopy(a132));
31         
32         List<Integer> a321 = ImmutableList.of(3, 2, 1);
33         System.out.println(intNatural.reverse().isOrdered(a321));
34         
35     }
36 }

参考文献:

  1. 官方文档: http://code.google.com/p/guava-libraries/wiki/ThrowablesExplained
  2. Google Guava学习之Ordering: http://blog.csdn.net/bloodlee_hust/article/details/6532187

你可能感兴趣的:(guava)