zoukankan      html  css  js  c++  java
  • [技术翻译]Guava官方文档Ordering

    关于排序

    Guava的链式比较器

    例子

    assertTrue(byLengthOrdering.reverse().isOrdered(list));

    梗概

    Ordering是Guava的链式比较器类,可以被用作构造复杂的比较器,并应用到collection对象。

    它的本质就仅仅是一个特殊的比较器实例。Ordering仅仅是执行一些依赖一个比较器的方法(譬如 Collections.max),并且把这些方法作为实例的的方法使用。Ordering类提供一些方法去调整和加强已经存在的比较器。

    怎样创建一个Ordering

    普通的orderings由下面的静态方法创建。

    Method

    Description

    natural()

    将能比较的对象按照自然顺序排序

    usingToString()

    使用toString()返回的字符串按字典顺序进行排序

    Ordering.from(Comparator)

    使用一个已经存在的比较器

    但是,去创建一个Ordering 更一般的方法是完全跳过Comparator,直接去继承一个Ordering抽象类。

     

    链式比较

    1 Ordering<String> byLengthOrdering = new Ordering<String>() {
    2   public int compare(String left, String right) {
    3     return Ints.compare(left.length(), right.length());
    4   }
    5 };

    给予一个Ordering就能被包装,去获得派生的Orderings.下面是一些经常用到的变体方法。

     

     

     

    Method

    Description

    reverse()

    返回一个顺序相反的Ordering.

    nullsFirst()

    返回一个Ordering.,非空值优先输出,其余和最初的Ordering一样

    compound(Comparator)

    当第一个比较器比较相等时使用第二个比较器。

    lexicographical()

    Returns an Ordering that orders iterables lexicographically by their elements.

    返回一个比较器,按照字典顺序遍历它的元素

    onResultOf(Function)

    返回一个比较器,它将函数应用到它的元素,然后按照最初的比较器将元素排序。

    譬如,当你想要一个能将下面的类比较的比较器。。。

    1 class Foo {
    2   @Nullable String sortedBy;
    3   int notSortedBy;
    4 }

    它能处理空的sortedBy。这儿是一种建立在链式方法的解决方案。

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

    当读到一个链式Ordering.的时候,从右往左看。上面的例子对Foo的实例排序是按照查找他们的属性值sortedBy,首先移动非空值到顶部,然后对他们的其余值进行排序。从后往前看是因为每一个链式调用都是包装前一个Ordering 成为一个新的Ordering 。

    (从后向前看法则的特例:若是链条中有compound,从左向后读。为了避免迷惑,不要将包含 compound的调用和别的链式调用混合在一起。)

    太长的链式不容易理解,我们建议像例子那样将链式限制在3个回调。即使如此,你仍可以像Function 的实例那样通过分割出中间对象来简化链式。

    1 Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(sortKeyFunction);

    关于应用

    Guava提供许多方法去利用ordering 处理和检查值或者集合。我们在下面列出以下一些经常用到的。

    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 theIterable is empty.

    max(Iterable)min(Iterator),max(Iterator)

  • 相关阅读:
    windows ip路由
    linux ip命令和ifconfig命令
    工作项目技术总结
    网络安全体系
    网络适配器输入的IP地址 已经分配给另一个适配器
    Android笔记之ImageView设置图片以灰色显示
    js小功能
    html2canvas截取图片跨域解决办法
    JS学习笔记(二).eq()与[]的区别
    jQuery中常用的元素查找方法总结
  • 原文地址:https://www.cnblogs.com/shibazijiang/p/3585362.html
Copyright © 2011-2022 走看看