Ordering - Application解析
一、简介
官网对Ordering - Application简介如下:
Guava provides a number of methods to manipulate or examine values or collections using the ordering
大致意思为Guava提供了许多方法使用Ordering操作或检查值或集合.并列出了最受欢迎的部分方法:
![](https://img.haomeiwen.com/i6163304/427e16ee1c322d01.png)
好了,废话不多说,直接进入实验.
二、Ordering的Application试验
实验列表如下:
step1 测试Application-greatestOf()
Ordering使用greatestOf()方法入参为iterable和一个int类型的size.将会得到通过ordering之后的指定size大小的集合并且集合元素为倒序.
简单源码解析:
/**
* Returns the {@code k} greatest elements of the given iterable according to this ordering, in
* order from greatest to least. If there are fewer than {@code k} elements present, all will be
* included.
*
* <p>The implementation does not necessarily use a <i>stable</i> sorting algorithm; when multiple
* elements are equivalent, it is undefined which will come first.
*
* <p><b>Java 8 users:</b> Continue to use this method for now. After the next release of Guava,
* use {@code Streams.stream(iterable).collect(Comparators.greatest(k, thisComparator))} instead.
*
* @return an immutable {@code RandomAccess} list of the {@code k} greatest elements in
* <i>descending order</i>
* @throws IllegalArgumentException if {@code k} is negative
* @since 8.0
*/
public <E extends T> List<E> greatestOf(Iterable<E> iterable, int k) {
// TODO(kevinb): see if delegation is hurting performance noticeably
// TODO(kevinb): if we change this implementation, add full unit tests.
return reverse().leastOf(iterable, k);
}
此处可以发现其实是调用了reverse().leastOf(iterable, k)方法.
/**
* Returns the {@code k} least elements of the given iterable according to this ordering, in order
* from least to greatest. If there are fewer than {@code k} elements present, all will be
* included.
*
* <p>The implementation does not necessarily use a <i>stable</i> sorting algorithm; when multiple
* elements are equivalent, it is undefined which will come first.
*
* <p><b>Java 8 users:</b> Continue to use this method for now. After the next release of Guava,
* use {@code Streams.stream(iterable).collect(Comparators.least(k, thisComparator))} instead.
*
* @return an immutable {@code RandomAccess} list of the {@code k} least elements in ascending
* order
* @throws IllegalArgumentException if {@code k} is negative
* @since 8.0
*/
public <E extends T> List<E> leastOf(Iterable<E> iterable, int k) {
if (iterable instanceof Collection) {
Collection<E> collection = (Collection<E>) iterable;
if (collection.size() <= 2L * k) {
// In this case, just dumping the collection to an array and sorting is
// faster than using the implementation for Iterator, which is
// specialized for k much smaller than n.
@SuppressWarnings("unchecked") // c only contains E's and doesn't escape
E[] array = (E[]) collection.toArray();
Arrays.sort(array, this);
if (array.length > k) {
array = ObjectArrays.arraysCopyOf(array, k);
}
return Collections.unmodifiableList(Arrays.asList(array));
}
}
return leastOf(iterable.iterator(), k);
}
最终发现当集合数量小于等于2时,用数组进行了比较.否则调用了leastOf(iterable.iterator(), k).
......
简单实验代码:
System.out.println("===========================测试ordering - Application实验======================");
System.out.println("===========================测试Application - greatestOf()实验======================");
Ordering<Integer> testApplicationOrdering = new Ordering<Integer>() {
@Override
public int compare(Integer left, Integer right) {
return Ints.compare(left, right);
}
};
ImmutableList<Integer> testList = ImmutableList.of(1, 6, 3, 2, 5, 4, 7);
System.out.println("Application-greatestOf()方法正常输出:" + JsonMoreUtils.toJson(testApplicationOrdering.greatestOf(testList, 3)));
System.out.println("Application-greatestOf()方法超出索引输出:" + JsonMoreUtils.toJson(testApplicationOrdering.greatestOf(testList, 8)));
System.out.println("Application-greatestOf()方法负数输出:" + JsonMoreUtils.toJson(testApplicationOrdering.greatestOf(testList, -1)));
实验结果:
===========================测试ordering - Application实验======================
Application-greatestOf()方法正常输出:[7,6,5]
Application-greatestOf()方法超出索引输出:[7,6,5,4,3,2,1]
Exception in thread "main" java.lang.IllegalArgumentException: k cannot be negative but was: -1
at com.google.common.collect.CollectPreconditions.checkNonnegative(CollectPreconditions.java:41)
at com.google.common.collect.Ordering.leastOf(Ordering.java:773)
at com.google.common.collect.Ordering.leastOf(Ordering.java:752)
at com.google.common.collect.Ordering.greatestOf(Ordering.java:812)
at com.toxic.anepoch.guava.parsing.ordering.TestOrdering.main(TestOrdering.java:43)
值得注意的是使用greatestOf()方法时,指定的集合中不要有null存在,否则出现NPE;指定的size不能为负数.
step2 测试Application-isOrdered()
Ordering使用isOrdered()方法入参为iterable,它会将传入的参数进行比较.
如果第一个元素后的每个元素都大于前一个元素.则返回true,否则返回false.
简单源码解析:
/**
* Returns {@code true} if each element in {@code iterable} after the first is greater than or
* equal to the element that preceded it, according to this ordering. Note that this is always
* true when the iterable has fewer than two elements.
*
* <p><b>Java 8 users:</b> Use the equivalent {@link Comparators#isInOrder(Iterable)} instead,
* since the rest of {@code Ordering} is mostly obsolete (as explained in the class
* documentation).
*/
public boolean isOrdered(Iterable<? extends T> iterable) {
Iterator<? extends T> it = iterable.iterator();
if (it.hasNext()) {
T prev = it.next();
while (it.hasNext()) {
T next = it.next();
if (compare(prev, next) > 0) {
return false;
}
prev = next;
}
}
return true;
}
简单实验代码:
//测试isOrdered方法
System.out.println("===========================测试Application - isOrdered()实验======================");
ImmutableList<Integer> testList = ImmutableList.of(1, 6, 3, 2, 5, 4, 7);
ImmutableList<Integer> testListOrdered1 = ImmutableList.of(1, 2, 3, 4, 5);
ImmutableList<Integer> testListOrdered2 = ImmutableList.of(3);
System.out.println("Application-isOrdered() false方法正常输出:" + JsonMoreUtils.toJson(testApplicationOrdering.isOrdered(testList)));
System.out.println("Application-isOrdered() true方法正常输出:" + JsonMoreUtils.toJson(testApplicationOrdering.isOrdered(testListOrdered1)));
System.out.println("Application-isOrdered() size<2方法正常输出:" + JsonMoreUtils.toJson(testApplicationOrdering.isOrdered(testListOrdered2)));
实验结果:
===========================测试Application - isOrdered()实验======================
Application-isOrdered() false方法正常输出:false
Application-isOrdered() true方法正常输出:true
Application-isOrdered() size<2方法正常输出:true
step3 测试Application-sortedCopy()
Ordering使用sortedCopy()方法入参为iterable,它会将指定元素的已排序副本作为列表返回。
简单源码解析:
/**
* Returns a <b>mutable</b> list containing {@code elements} sorted by this ordering; use this
* only when the resulting list may need further modification, or may contain {@code null}. The
* input is not modified. The returned list is serializable and has random access.
*
* <p>Unlike {@link Sets#newTreeSet(Iterable)}, this method does not discard elements that are
* duplicates according to the comparator. The sort performed is <i>stable</i>, meaning that such
* elements will appear in the returned list in the same order they appeared in {@code elements}.
*
* <p><b>Performance note:</b> According to our
* benchmarking
* on Open JDK 7, {@link #immutableSortedCopy} generally performs better (in both time and space)
* than this method, and this method in turn generally performs better than copying the list and
* calling {@link Collections#sort(List)}.
*/
// TODO(kevinb): rerun benchmarks including new options
@CanIgnoreReturnValue // TODO(kak): Consider removing this
public <E extends T> List<E> sortedCopy(Iterable<E> elements) {
@SuppressWarnings("unchecked") // does not escape, and contains only E's
E[] array = (E[]) Iterables.toArray(elements);
Arrays.sort(array, this);
return Lists.newArrayList(Arrays.asList(array));
}
简单实验代码:
System.out.println("===========================测试Application - sortedCopy()实验======================");
List<Integer> testSortedList = Lists.newArrayList(1,5,2,4,3,6);
testApplicationOrdering.sortedCopy(testSortedList);
System.out.println("Application-sortedCopy()方法正常输出:" + JsonMoreUtils.toJson(testApplicationOrdering.sortedCopy(testSortedList)));
实验结果:
===========================测试Application - sortedCopy()实验======================
Application-sortedCopy()方法正常输出:[1,2,3,4,5,6]
step4 测试Application-min()
Ordering使用min()方法入参为iterable,它会根据此顺序返回其参数的最小值。如果有多个最小值,则返回第一个最小值。
简单实验代码:
System.out.println("===========================测试Application - min()实验======================");
ImmutableList<Integer> testMinList = ImmutableList.of(1,3,4,6,2,5);
System.out.println("Application-min()方法正常输出:" + JsonMoreUtils.toJson(testApplicationOrdering.min(testMinList)));
System.out.println("Application-min()方法正常输出:" + JsonMoreUtils.toJson(testApplicationOrdering.min(3, 7)));
System.out.println("Application-min()方法正常输出:" + JsonMoreUtils.toJson(testApplicationOrdering.min(2, 3, 4, 5)));
实验结果:
===========================测试Application - min()实验======================
Application-min()方法正常输出:1
Application-min()方法正常输出:3
Application-min()方法正常输出:2
=======================================
当然除了以上列举的之外,还有其他的方法,如:immutableSortedCopy()、isStrictlyOrdered()等.
网友评论