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);
  }
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 ) { @ SuppressWarnings ( "unchecked" ) 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 ) ; }
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