0

What's the best way of getting the Kth largest element in a DoubleStream in java?

I know we can do .max().getDouble() to get the largest element.

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
nedlaback
  • 134
  • 1
  • 1
  • 11
  • 2
    Did you try to search for it? There are plenty of questions dealing with finding the k-th largest element in various data structures. – Henry Oct 19 '17 at 04:04
  • 2
    [No code](http://idownvotedbecau.se/nocode) and [no attempt](http://idownvotedbecau.se/noattempt), please see [ask]. – Alex Oct 19 '17 at 04:06
  • Possible duplicate of [How to find the kth largest element in an unsorted array of length n in O(n)?](https://stackoverflow.com/questions/251781/how-to-find-the-kth-largest-element-in-an-unsorted-array-of-length-n-in-on) – geocar Oct 19 '17 at 15:01
  • How can this be too broad? – ZhekaKozlov Oct 20 '17 at 02:31

4 Answers4

2
OptionalDouble kthLargest = stream
        .map(i -> -i) // Trick to sort in reverse order
        .sorted()
        .distinct() // Remove duplicates
        .map(i -> -i)
        .skip(k - 1)
        .findFirst();
ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
  • @Holger Stream may contain less than `k` elements – ZhekaKozlov Oct 19 '17 at 07:41
  • @Holger would this solution better faster/more memory efficient than using a boxed stream to sorted descending (which might be easier to read)? – Malte Hartwig Oct 19 '17 at 07:57
  • 2
    @Malte Hartwig: that depends on the stream size and on the implementation. AFAIK, when using `distinct()` with the current implementation, you have the boxing overhead anyway. But if you really need this operation to be efficient, you should use neither, but rather do something [like this](https://stackoverflow.com/q/5380568/2711488); it’s the same for the k smallest or largest elements. – Holger Oct 19 '17 at 08:08
  • What requirement is satisfied by calling `distinct()`? – shmosel Oct 19 '17 at 18:40
  • 1
    @shmosel some commenters asked how duplicate entries should be treated: Should the second largest element of [1,2,3,3] be 3, or 2? Since NeedleBallista hadn't answered in time, the distinct call was added as an option to ignore duplicates. – Malte Hartwig Oct 20 '17 at 07:11
2
doubleStream.boxed()
            .sorted(Comparator.reverseOrder())
            .skip(k - 1)
            .findFirst()
            .orElse(null);

Will give you the kth largest element, or null if there are less than k elements in the stream.

Malte Hartwig
  • 4,477
  • 2
  • 14
  • 30
0

Sort the stream, transform it to an array and get the item at array.length-k.

Example:

private static double getKth(DoubleStream ds, int k) {
    double[] array = ds
        .sorted()
        .toArray();
    return array[array.length-k];
}

You should add validation to make sure k has a legit value

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
-1

Once you have DoubleStream, make it sorted() in reverse order (DESC). Then limit(k) and sort ASC and take the first.

.boxed()
.distinct() //in case you want to ignore repeating values
.sorted(Comparator.reverseOrder())
.limit(k)
.sorted()
.findFirst());
xenteros
  • 15,586
  • 12
  • 56
  • 91