Chapter 3 -- Ordering
explained
Example 例子
assertTrue(byLengthOrdering.reverse().isOrdered(list));
Overview 总览
Ordering is Guava's "fluent" Comparator class, which can be used to build complex comparators and apply them to collections of objects.
Ordering是Guava的"fluent"比较器类, 他可以用来组件复杂的比较器并应用到集合中
At its core, an Ordering instance is nothing more than a special Comparator instance. Ordering simply takes the methods that rely on aComparator (for example, Collections.max) and makes them available as instance methods. For additional power, Ordering class provides chaining methods to tweak and enhance existing comparators.
它的核心内, 一个 Ordering 实例只不过是一个特殊的比较器实例, Ordering简单地让这些方法依赖一个comparator(例如 Collections.max) 并使它们作为实例方法成为可能. 另外,Ordering提供了链式方法调用来调整和提高现有的comparator的能力
Creation 创建
Common orderings are provided by static methods:
通常ordering由以下静态方法提供:
| Method | Description |
| natural() |
Uses the natural ordering on Comparable types. 在可比较的类型上使用natural ordering, 这个方法返回一个NaturalOrdering, 它的比较方法使用了对象默认的compareTo实现 |
| usingToString() |
Compares objects by the lexicographical ordering of their string representations, as returned by toString(). 通过词典编撰的字符串顺序来比较两个object, 使用的是对象的Object.toString().CompareTo()方法来比较 |
Making a preexisting Comparator into an Ordering is as simple as using Ordering.from(Comparator).
使用一个已经存储在的comparator来生成Ordering很简单: Ordering.from(Comparator)
But the more common way to create a custom Ordering is to skip the Comparator entirely in favor of extending the Ordering abstract class directly:
更常用的创建自定义Ordering的方法是为了直接支持继承Ordering完全跳过Comparator:
Ordering<String> byLengthOrdering =new Ordering<String>(){
publicint compare(String left,String right){
returnInts.compare(left.length(), right.length());
}
};
Chaining 链式调用
A given Ordering can be wrapped to obtain derived orderings. Some of the most commonly used variations include:
一个Ordering可以被包装成派生的ordering. 一些最常用的变体如下:
| Method | Description |
| reverse() |
Returns the reverse ordering. 返回逆向的ordering, 相当于本来是left.compareTo(right), 现在是 right.compareTo(left) |
| nullsFirst() |
Returns an Ordering that orders nulls before non-null elements, and otherwise behaves the same as the original Ordering. See also nullsLast(). 返回一个先排序nulls在排序non-null元素的Ordering, 其他方面和原始的Ordering一样 |
| compound(Comparator) |
Returns an Ordering which uses the specified Comparator to "break ties." 返回一个使用指定comparator的Ordering, 这个指定的comparator会作为第二比较器来使用 |
| lexicographical() |
Returns an Ordering that orders iterables lexicographically by their elements. 返回一个根据元素的字典顺序来排序遍历器(iterables)的ordering. 例如, [] < [1] < [1, 1] < [1, 2] < [2] |
| onResultOf(Function) |
Returns an Ordering which orders values by applying the function to them and then comparing the results using the original Ordering. 返回一个先用function的apply方法处理待排序的值然后再比较处理后的结果的Ordering |
For example, let's say you want a comparator for the class...
举个例子, 比如你想要一个class的comparator
classFoo{
@Nullable String sortedBy;
int notSortedBy;
}
...that can deal with null values of sortedBy. Here is a solution built atop the chaining methods:
这可以处理sortedBy的null值.这里有一个使用链式调用的解决方案:
Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(new Function<Foo,String>(){
publicString apply(Foo foo){
return foo.sortedBy;
}
});
When reading a chain of Ordering calls, work "backward" from right to left. The example above orders Foo instances by looking up theirsortedBy field values, first moving any null sortedBy values to the top and then sorting the remaining values by natural string ordering. This backward order arises because each chaining call is "wrapping" the previous Ordering into a new one.
当读取一个链式的ordering调用, 工作顺序是用右向左"反向"的. 上面的例子的顺序是先查找sortedBy的值, 然后将null的sortedBy的值移到最前面,然后将剩下的值按照自然字符串顺序进行排序.这种反向顺序出现是因为每个链的调用都被将一个Ordering包装成一个新的Ordering.
(Exception to the "backwards" rule: For chains of calls to compound, read from left to right. To avoid confusion, avoid intermixing compound calls with other chained calls.)
("反向"规则的例外: 对compound的连时调用, 是从左到右的.为了避免困惑, 避免将compound和其他链式方法混在一起调用)
Chains longer than a few calls can be difficult to understand. We recommend limiting chaining to about three calls as in the example above. Even then, you may wish to simplify the code by separating out intermediate objects such as Function instances:
太长的链式调用会让人难以理解.我们建议将链式调用限制在三个方法内.即使那样,你也可能希望通过分离中间对象(例如Function匿名类)来简化代码:
Ordering<Foo> ordering =Ordering.natural().nullsFirst().onResultOf(sortKeyFunction);
Application 应用
Guava provides a number of methods to manipulate or examine values or collections using the ordering. We list some of the most popular here.
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. 返回指定的iterable最大的k个元素, 根据当前ordering的规则, 顺序是从最大到最小的. |
leastOf |
| isOrdered(Iterable) |
Tests if the specified Iterable is in nondecreasing order according to this ordering. 测试指定的Iterable是否按照ordering的非降序顺序排列 |
isStrictlyOrdered |
| sortedCopy(Iterable) |
Returns a sorted copy of the specified elements as a List. 返回一个Iterable的排序副本 |
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. 返回根据ordering判断的两个参数中的最小值.假如两个参数一样大,则返回第一个参数 |
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. 返回根据ordering判断的多个参数中的最小值.如果有多个最小值,则返回第一个参数 |
max(E, E, E, E...) |
| min(Iterable) |
Returns the minimum element of the specified Iterable. Throws a NoSuchElementException if the Iterable is empty. 返回Iterable中的最小值.如果Iterable是空的则抛出NoSuchElementException异常 |
max(Iterable), min(Iterator),max(Iterator) |
Chapter 3 -- Ordering的更多相关文章
- CHAPTER 19 Ordering the World 第19章 分类世界
CHAPTER 19 Ordering the World 第19章 分类世界 Our planet is home to a bewildering variety of plants and an ...
- Think Python - Chapter 18 - Inheritance
In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...
- Learning Puppet — Resource Ordering
Learning Puppet — Resource Ordering Learn about dependencies and refresh events, manage the relation ...
- MongoDB:The Definitive Guide CHAPTER 2 Getting Started
MongoDB is very powerful, but it is still easy to get started with. In this chapter we’ll introduce ...
- Chapter 4: Spring and AOP:Spring's AOP Framework -- draft
Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...
- Chapter 3. Programming with RDDs
Programming with RDDs This chapter introduces Spark's core abstraction for working with data, the r ...
- Chapter 2 Secondary Sorting:Detailed Example
2.1 Introduction MapReduce framework sorts input to reducers by key, but values of reducers are arbi ...
- JVM Specification 9th Edition (4) Chapter 3. Compiling for the Java Virtual Machine
Chapter 3. Compiling for the Java Virtual Machine 内容列表 3.1. Format of Examples 3.2. Use of Constants ...
- JVM Specification 9th Edition (3) Chapter 2. The Structure of the Java Virtual Machine
Chapter 2. The Structure of the Java Virtual Machine 内容列表 2.1. The class File Format (class文件的格式) 2. ...
随机推荐
- 【Java】 大话数据结构(2) 线性表之单链表
本文根据<大话数据结构>一书,实现了Java版的单链表. 每个结点中只包含一个指针域的链表,称为单链表. 单链表的结构如图所示: 单链表与顺序存储结构的对比: 实现程序: package ...
- Qt编译好的oracle驱动下载
在上文,我累赘了一大堆,给大家写了一篇Qt如何编译OCI驱动,在这里自然就不再累赘了,直接附上编译好的文件供大家下载: <Qt5.3.1+OCI驱动下载地址> 有经济来源的请传送:http ...
- Lighthouse前端性能优化测试工具
在前端开发中,对于自己开发的app或者web page性能的好坏,一直是让前端开发很在意的话题.我们需要专业的网站测试工具,让我们知道自己的网页还有哪些需要更为优化的方面,我自己尝试了一款工具:Lig ...
- Sublime快速入门
在当前的互联网时代,任何程序语言和相关技术都只是实现互联网应用的一种手段,这也就造成了大量的互联网工程师长期与不同的语言.技术.系统环境.IDE等打交道.因此一个相对统一方便的IDE对于程序员来说显得 ...
- MySQL服务器SSD性能问题分析与测试
[问题] 我们有台HP的服务器,SSD在写IOPS约5000时,%util达到80%以上,那么这块SSD的性能究竟有没有问题,为解决这个问题做了下面测试. [工具] blktrace是linux下用来 ...
- Redis介绍及部署在CentOS7上(一)
0.Redis目录结构 1)Redis介绍及部署在CentOS7上(一) 2)Redis指令与数据结构(二) 3)Redis客户端连接以及持久化数据(三) 4)Redis高可用之主从复制实践(四) 5 ...
- JAVA中的email正则表达式
说到正则表达式,网上有很多的通用的表达式,可是事实上说来,一般人的都 不愿意去拿来研究,就是拿来就直接用就行了.可是,事实上,可能有些时候,项目中或公司里的实际情况不一样,得要修改一下正则表达式的,根 ...
- 在windows下安装配置python开发环境及Ulipad开发工具(转)
最近开始学习Python,在网上寻找一下比较好的IDE.因为以前用C#做开发的,用Visual Studio作为IDE,鉴于用惯了VS这么强大的IDE,所以对IDE有一定的依赖性. Python的ID ...
- 自己封装jquery的一些方法 链式调用模式
function getIndex(ele){ var parent=ele.parentNode; var brothers=parent.children; for(var i=0,len=bro ...
- Windows 10官方原版ISO制作方法
其实市面上的ISO原版都是这样的方法制作成光盘,然后再打包出来供人们下载的. 1.下载Windows 10安装程序工具: https://www.microsoft.com/zh-cn/softwar ...