95

In a previous question [ How to dynamically do filtering in Java 8? ] Stuart Marks gave a wonderful answer, and provided several useful utilities to handle selection of topN and topPercent from stream.

I'll include them here from his original answer :

@FunctionalInterface
public interface Criterion {
    Stream<Widget> apply(Stream<Widget> s);
}

Criterion topN(Comparator<Widget> cmp, long n) {
    return stream -> stream.sorted(cmp).limit(n);
}

Criterion topPercent(Comparator<Widget> cmp, double pct) {
    return stream -> {
        List<Widget> temp =
            stream.sorted(cmp).collect(toList());
        return temp.stream()
                   .limit((long)(temp.size() * pct));
    };
}

My questions here are :

[1] How to get top items from 3 to 7 from a stream with certain amount of items, so if the stream has items from A1, A2 .. A10, the call to

topNFromRange(Comparator<Widget> cmp, long from, long to) = topNFromRange(comparing(Widget::length), 3L, 7L)

will return { A3, A4, A5, A6, A7 }

The simplest way I can think of is get the top 7 [ T7 ] from original, get the top 3 [ T3 ] from original, and then get T7 - T3.

[2] How to get top items from top 10% to top 30% from a stream with certain amount of items, so if the stream has items from X1, X2 .. X100, the call to

topPercentFromRange(Comparator<Widget> cmp, double from, double to) = topNFromRange(comparing(Widget::length), 0.10, 0.30)

will return { X10, X11, X12, ..., X29, X30 }

The simplest way I can think of is get the top 30% [ TP30 ] from original, get the top 10% [ TP10 ] from original, and then get TP30 - TP10.

What are some better ways to use Java 8 Lambda to concisely express the above situations ?

2 Answers 2

87

To get a range from a Stream<T>, you can use skip(long n) to first skip a set number of elements, and then you can call limit(long n) to only take a specific amount of items.

Consider a stream with 10 elements, then to get elements 3 to 7, you would normally call from a List:

list.subList(3, 7);

Now with a Stream, you need to first skip 3 items, and then take 7 - 3 = 4 items, so it becomes:

stream.skip(3).limit(4);

As a variant to @StuartMarks' solution to the second answer, I'll offer you the following solution which leaves the possibility to chain intact, it works similar to how @StuartMarks does it:

private <T> Collector<T, ?, Stream<T>> topPercentFromRangeCollector(Comparator<T> comparator, double from, double to) {
    return Collectors.collectingAndThen(
        Collectors.toList(),
        list -> list.stream()
            .sorted(comparator)
            .skip((long)(list.size() * from))
            .limit((long)(list.size() * (to - from)))
    );
}

and

IntStream.range(0, 100)
        .boxed()
        .collect(topPercentFromRangeCollector(Comparator.comparingInt(i -> i), 0.1d, 0.3d))
        .forEach(System.out::println);

This will print the elements 10 through 29.

It works by using a Collector<T, ?, Stream<T>> that takes in your elements from the stream, transforms them into a List<T>, then obtains a Stream<T>, sorts it and applies the (correct) bounds to it.

5
  • If you skip the first 10% of items, then there is only 90% items left in the stream, how to get the items from the original 30%, because the 30% from the 90% is not the original 30%, am I correct ?
    – Frank
    Apr 7, 2014 at 16:01
  • 1
    @Frank You will need to calculate those numbers in advance.
    – skiwi
    Apr 7, 2014 at 16:33
  • @Frank I have updated the answer to also include a variant that works with chaining streams together.
    – skiwi
    Apr 8, 2014 at 8:57
  • Interesting and thanks for your effort. So what's the difference from a user standpoint ? When to use which one ? Any efficiency/accuracy differences ? What I can see is more steps involved and seems more complex, is there a way to simplify it to the form : Criterion topPercentFromRange(Comparator<Widget> cmp, double from, double to) , which is more intuitive.
    – Frank
    Apr 8, 2014 at 15:59
  • @Frank I would personally prefer to chain methods, as that are the essentials of using streams, I do not like static methods, unless absolutely neccessary. That is the only real difference as far as I reckon.
    – skiwi
    Apr 8, 2014 at 16:00
67

User skiwi already answered the first part of the question. The second part is:

(2) How to get top items from top 10% to top 30% from a stream with certain amount of items....

To do this, you have to use a similar technique as topPercent in my answer to the other question. That is, you have to collect the elements into a list in order to be able to get a count of the elements, possibly after some upstream filtering has been done.

Once you have the count, then you compute the right values for skip and limit based on the count and the percentages you want. Something like this might work:

Criterion topPercentFromRange(Comparator<Widget> cmp, double from, double to) {
    return stream -> {
        List<Widget> temp =
            stream.sorted(cmp).collect(toList());
        return temp.stream()
                   .skip((long)(temp.size() * from))
                   .limit((long)(temp.size() * (to - from)));
    };
}

Of course you will have to do error checking on from and to. A more subtle problem is determining how many elements to emit. For example, if you have ten elements, they are at indexes [0..9], which correspond to 0%, 10%, 20%, ..., 90%. But if you were to ask for a range from 9% to 11%, the above code would emit no elements at all, not the one at 10% like you might expect. So some tinkering with the percentage computations is probably necessary to fit the semantics of what you're trying to do.

3
  • Close enough to what I was looking for, I'll work out the details, thanks !
    – Frank
    Apr 7, 2014 at 23:04
  • I have updated my answer to also include a form of what you are doing, but then with using Collectors, perhaps it could also be interesting for the original question of criteria?
    – skiwi
    Apr 8, 2014 at 8:52
  • 1
    @skiwi Interesting, using the finisher function of a collector to turn the collection back into a stream. I'm not sure whether it's necessarily better or worse than just declaring a local variable. (The lambda parameter is used like a local in this case.) It's a useful technique to keep in mind for the future, though. Apr 8, 2014 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.