guava/src/com/google/common/collect/TopKSelector.java
public List<T> topK() {
    Arrays.sort(buffer, 0, bufferSize, comparator);
    if (bufferSize > k) {
      Arrays.fill(buffer, k, buffer.length, null);
      bufferSize = k;
      threshold = buffer[k - 1];
    }
    // we have to support null elements, so no ImmutableList for us
    return Collections.unmodifiableList(Arrays.asList(Arrays.copyOf(buffer, bufferSize)));
  }
public List<T> topK() {
    Arrays.sort(buffer, 0, bufferSize, comparator);
    if (bufferSize > k) {
      Arrays.fill(buffer, k, buffer.length, null);
      bufferSize = k;
      threshold = buffer[k - 1];
    }
    // we have to support null elements, so no ImmutableList for us
    return Collections.unmodifiableList(Arrays.asList(Arrays.copyOf(buffer, bufferSize)));
  }
Returns the top {@code k} elements offered to this {@code TopKSelector}, or all elements if
fewer than {@code k} have been offered, in the order specified by the factory used to create
this {@code TopKSelector}.

<p>The returned list is an unmodifiable copy and will not be affected by further changes to
this {@code TopKSelector}. This method returns in O(k log k) time.