1. Introduction
1.介绍
This cookbook illustrates how to use the Guava style Ordering and Comparators. It is continuing the cookbook and example focus format that I started in the previous post about Guava collections.
这本食谱说明了如何使用Guava风格的排序和比较器。它延续了我在上一篇关于Guava集合的文章中开始的cookbook和示例焦点格式。
2. The Cookbook
2.食谱
dealing with nulls in a Collection
处理集合中的空值。
nulls first
首先是空号。
List<Integer> toSort = Arrays.asList(3, 5, 4, null, 1, 2);
Collections.sort(toSort, Ordering.natural().nullsFirst());
assertThat(toSort.get(0), nullValue());
nulls last
最后的空位。
List<Integer> toSort = Arrays.asList(3, 5, 4, null, 1, 2);
Collections.sort(toSort, Ordering.natural().nullsLast());
assertThat(toSort.get(toSort.size() - 1), nullValue());
natural ordering
自然订购
List<Integer> toSort = Arrays.asList(3, 5, 4, 1, 2);
Collections.sort(toSort, Ordering.natural());
assertTrue(Ordering.natural().isOrdered(toSort));
chaining 2 orderings
链接2个订单
List<Integer> toSort = Arrays.asList(3, 5, 4, 1, 2);
Collections.sort(toSort, Ordering.natural().reverse());
reverse an ordering
逆向排序
List<Integer> toSort = Arrays.asList(3, 5, 4, null, 1, 2);
Collections.sort(toSort, Ordering.natural().nullsLast().reverse());
assertThat(toSort.get(0), nullValue());
custom order – Strings by length
自定义顺序–按长度排列的字符串
private class OrderingByLenght extends Ordering<String> {
@Override
public int compare(String s1, String s2) {
return Ints.compare(s1.length(), s2.length());
}
}
List<String> toSort = Arrays.asList("zz", "aa", "b", "ccc");
Ordering<String> byLength = new OrderingByLenght();
Collections.sort(toSort, byLength);
Ordering<String> expectedOrder = Ordering.explicit(Lists.newArrayList("b", "zz", "aa", "ccc"));
assertTrue(expectedOrder.isOrdered(toSort))
checking explicit order
检查明确的订单
List<String> toSort = Arrays.asList("zz", "aa", "b", "ccc");
Ordering<String> byLength = new OrderingByLenght();
Collections.sort(toSort, byLength);
Ordering<String> expectedOrder = Ordering.explicit(Lists.newArrayList("b", "zz", "aa", "ccc"));
assertTrue(expectedOrder.isOrdered(toSort));
checking string ordering
检查字符串排序
List<Integer> toSort = Arrays.asList(3, 5, 4, 2, 1, 2);
Collections.sort(toSort, Ordering.natural());
assertFalse(Ordering.natural().isStrictlyOrdered(toSort));
secondary ordering
二次订购
List<String> toSort = Arrays.asList("zz", "aa", "b", "ccc");
Ordering<String> byLength = new OrderingByLenght();
Collections.sort(toSort, byLength.compound(Ordering.natural()));
Ordering<String> expectedOrder = Ordering.explicit(Lists.newArrayList("b", "aa", "zz", "ccc"));
assertTrue(expectedOrder.isOrdered(toSort));
complex custom ordering example – with chaining
复杂的自定义订购示例–带连锁的
List<String> toSort = Arrays.asList("zz", "aa", null, "b", "ccc");
Collections.sort(toSort,
new OrderingByLenght().reverse().compound(Ordering.natural()).nullsLast());
System.out.println(toSort);
sort using toString representation
使用toString表示进行排序
List<Integer> toSort = Arrays.asList(1, 2, 11);
Collections.sort(toSort, Ordering.usingToString());
Ordering<Integer> expectedOrder = Ordering.explicit(Lists.newArrayList(1, 11, 2));
assertTrue(expectedOrder.isOrdered(toSort));
sort, then find (binary search)
排序,然后查找(二进制搜索)
List<Integer> toSort = Arrays.asList(1, 2, 11);
Collections.sort(toSort, Ordering.usingToString());
int found = Ordering.usingToString().binarySearch(toSort, 2);
System.out.println(found);
find min/max without having to sort (faster)
找到最小/最大值,而无需排序(更快)
List<Integer> toSort = Arrays.asList(2, 1, 11, 100, 8, 14);
int found = Ordering.usingToString().min(toSort);
assertThat(found, equalTo(1));
creating a sorted copy of the list from an ordering
从一个排序中创建一个排序的列表副本
List<String> toSort = Arrays.asList("aa", "b", "ccc");
List<String> sortedCopy = new OrderingByLenght().sortedCopy(toSort);
Ordering<String> expectedOrder = Ordering.explicit(Lists.newArrayList("b", "aa", "ccc"));
assertFalse(expectedOrder.isOrdered(toSort));
assertTrue(expectedOrder.isOrdered(sortedCopy));
creating a sorted partial copy – the least few elements
创建一个排序的部分副本–最少的几个元素
List<Integer> toSort = Arrays.asList(2, 1, 11, 100, 8, 14);
List<Integer> leastOf = Ordering.natural().leastOf(toSort, 3);
List<Integer> expected = Lists.newArrayList(1, 2, 8);
assertThat(expected, equalTo(leastOf));
ordering via intermediary Function
通过中介机构订购
List<Integer> toSort = Arrays.asList(2, 1, 11, 100, 8, 14);
Ordering<Object> ordering = Ordering.natural().onResultOf(Functions.toStringFunction());
List<Integer> sortedCopy = ordering.sortedCopy(toSort);
List<Integer> expected = Lists.newArrayList(1, 100, 11, 14, 2, 8);
assertThat(expected, equalTo(sortedCopy));
– note: the sort logic will first run the numbers through the function – transforming them into Strings – then sort with natural ordering on the Strings
– 注意:排序逻辑将首先通过函数运行数字–将其转化为字符串–然后对字符串进行自然排序。
3. More Guava Cookbooks
3.更多的番石榴烹饪书
Guava is a comprehensive and fantastically useful library – here’s a few more APIs covered in cookbook form:
Guava是一个全面的、非常有用的库–这里还有一些以菜谱形式介绍的API。
Enjoy.
请享受。
4. Conclusion
4.结论
This experimental format – the cookbook – has a clear focus – simplicity, and speed, so most recipes have no additional explanation other than the code example itself.
这种实验性的格式–食谱–有一个明确的重点–简单、快速,所以大多数食谱除了代码实例本身外,没有其他解释。
And as I mentioned before – this as a living document – new examples and use cases are welcome in the comments, and I will continue adding my own as I run into them.
正如我之前提到的–这是一份活的文件–欢迎在评论中提出新的例子和用例,我将继续添加我自己遇到的例子。
The implementation of all these examples and code snippets can be found over on GitHub – this is a Maven-based project, so it should be easy to import and run as it is.