/** * Returns the {@code k} greatest elements from the given iterator 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(iterator).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 14.0 */ public <E extends T> List<E> greatestOf(Iterator<E> iterator, int k) { return reverse().leastOf(iterator, k); }
/** * 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> 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); }
return leastOf(iterable.iterator(), k);