Peek the next Element in a stream

ctst :

Is there a way to peek the next element in a stream? The idea rose from a stream of a list of objects, where two following objects should be compared (to smooth some diffs, but that shouldn't matter here). As an old for loop this would look like:

List<Car> autobahn = getCars();
for (int i = 0; i < autobahn.size()-1; i++) {
    if(autobahn.get(i).speed>autobahn.get(i+1).speed)
        autobahn.get(i).honk();
}

The best way so far as stream would be:

autobahn.stream()
            .limit(autobahn.size()-1)
            .filter(car -> car.speed < autobahn.get(autobahn.indexOf(car)+1).speed)
            .forEach(car -> car.honk());

The main-problem with this solution is the indexOf method, since there might be twice the same car on the autobahn. A better solution would be some way to peek the next (or the one before) element (with an helping class, this might be even possible, but looks horrible)

BoxedCar boxedCar = new BoxedCar(autobahn.get(0));
autobahn.stream()
            .skip(1)
            .filter(car -> boxedCar.setContent(car))
            .forEach(car -> car.winTheRace());

with helperclass

class BoxedCar {

    Car content;

    BoxedCar(Car content) {
        this.content = content;
    }
    boolean setContent(Car content) {
        double speed = this.content.speed;
        this.content = content;
        return content.speed > speed;
    }
}

or to divert the Stream<Car> into a kind of Stream<(Car,Car)> with the second stream somehow created by the first one (this sounds also awful and here I have no idea, how this would look).

Is there a nice way to do this with streams, or are we stuck to the for-loop?

Tunaki :

Sticking with the for loop wouldn't be a bad idea. The Stream API isn't designed for this type of requirement. You can refer to that answer for more insight.

However, a simple way to do this using the Stream API would be to use a Stream over the indexes of your list, supposing that you have random access.

IntStream.range(0, autobahn.size() - 1)
         .filter(i -> autobahn.get(i).speed > autobahn.get(i+1).speed)
         .forEach(i -> autobahn.get(i).honk());

Note that this highly resemble the for loop.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Peek at next element in a Tree set using an iterator

Is it possible to peek at the next element in a range-for loop?

How do I "peek" the next element on a Java Scanner?

Is it possible to get next element in the Stream?

Advance next() to peek() with multipeek

Peek Value in Next Column

Kotlin Stream peek(...) method

Java stream peek() operation

Is it possible to peek at the next rand value

Method peek() shows different results in stream (peek() and findAny() together)

peek and element in Java's LinkedList

How do I continue with next element in the Java Stream in case of exception?

Different types of string.chars(), for .next() and .peek()

How to look/peek at previous or next rows

How to peek at the next value in a Javascript Iterator

idangerous slider styling peek preview for prev/next

Datatables - How to peek at next few rows?

python motor mongo cursor length or peek next

Stream.peek() can be skipped for optimization

Confused on Java Stream Results using peek and findAny

Better way than Stream.peek()

Java 8 stream peek and limit interaction

Why stream peek does not work properly in HashMap

In java, why stream peek is not working for me?

Peek at httppostedbase memory stream without clearing the buffer

Is there a way to make StreamExt::next non blocking (fail fast) if the stream is empty (need to wait for the next element)?

Next element in a for

Is there a built-in way to check if #next or #peek will raise StopIteration?

Peek previous/next cells in UICollectionView with native paging enabled?