Guava学习笔记(4)--Ordering简介[翻译+学习笔记]
2012-10-14 21:35
399 查看
Ordering是Guava最常用的Comparator类, 可以用来操作, 扩展和使用comparators.
自定义比较器, 进行排序判断, 进行排序, 获取最大最小值, 获取最大最小的前几个值等方法.
几个常用的static方法, 提供了三种常用的比较器
自定义Ordering
操作方法
reverse()方法, 获取了Ordering的反排序. 使用自定义的Ordering判断collection是否符合自定义顺序.
下面是使用
还有一些很有用的方法
最后以一个例子结尾吧, 都很简单, 只是简单的使用api, 这些方法的命名可以看到其用途, 好的命名方法可以见名知义
参考文献:
官方文档: http://code.google.com/p/guava-libraries/wiki/ThrowablesExplained
Google Guava学习之Ordering: http://blog.csdn.net/bloodlee_hust/article/details/6532187
自定义比较器, 进行排序判断, 进行排序, 获取最大最小值, 获取最大最小的前几个值等方法.
几个常用的static方法, 提供了三种常用的比较器
方法 | 描述 |
natural() | 使用Comparable类型的自然顺序, 例如, 整数从小到大, 字符串是按字典顺序. |
usingToString() | 使用toString()返回的字符串按字典顺序进行排序 |
arbitrary() | 返回一个所有对象的任意顺序, 即compare(a, b) == 0 就是 a == b (identity equality). 本身的排序是没有任何含义, 但是在VM的生命周期是一个常量. |
import java.util.Arrays; import java.util.List; import com.google.common.collect.Ordering; import com.google.common.primitives.Ints; public class Demo { public static void main(String[] args) { Ordering<String> byLengthOrdering = new Ordering<String>() { public int compare(String left, String right) { return Ints.compare(left.length(), right.length()); } }; List<String> strList = Arrays.asList("abc", "a", "bcd"); System.out.println(byLengthOrdering.reverse().isOrdered(strList)); List<String> strList1 = Arrays.asList("a", "ab", "bcd"); System.out.println(byLengthOrdering.isOrdered(strList1)); } }
操作方法
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) |
import java.util.List; import com.google.common.collect.ImmutableList; import com.google.common.collect.Ordering; public class NaturalOrderingDemo { public static void main(String[] args) { Ordering<String> natural = Ordering.natural(); List<String> abc = ImmutableList.of("a", "b", "c"); System.out.println(natural.isOrdered(abc)); List<String> cab = ImmutableList.of("c", "a", "b"); System.out.println(natural.isOrdered(cab)); System.out.println(cab = natural.sortedCopy(cab)); System.out.println(natural.max(cab)); System.out.println(natural.min(cab)); System.out.println(natural.leastOf(cab, 2)); System.out.println(natural.greatestOf(cab, 2)); System.out.println("===================================="); Ordering<Integer> intNatural = Ordering.natural(); List<Integer> a123 = ImmutableList.of(1, 2, 3); System.out.println(intNatural.isOrdered(a123)); List<Integer> a132 = ImmutableList.of(1, 3, 2); System.out.println(intNatural.isOrdered(a132)); System.out.println(intNatural.sortedCopy(a132)); List<Integer> a321 = ImmutableList.of(3, 2, 1); System.out.println(intNatural.reverse().isOrdered(a321)); } }
参考文献:
官方文档: http://code.google.com/p/guava-libraries/wiki/ThrowablesExplained
Google Guava学习之Ordering: http://blog.csdn.net/bloodlee_hust/article/details/6532187
相关文章推荐
- Guava学习笔记:Ordering犀利的比较器
- Guava学习笔记:Google Guava 类库简介
- JAVA学习笔记47——其他容器简介4:guava之MulitiSet、MultiMap、BiMap、Table
- Guava学习笔记【1】:Google Guava 类库简介
- Guava学习笔记(3)--覆写Object的常用方法[翻译+学习笔记]
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- 【转载】Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记(6)-- ImmutableCollections简介