Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Java 8 streams to find all values preceding a larger value?

Use Case

Through some coding Katas posted at work, I stumbled on this problem that I'm not sure how to solve.

Using Java 8 Streams, given a list of positive integers, produce a list of integers where the integer preceded a larger value.

[10, 1, 15, 30, 2, 6] 

The above input would yield:

[1, 15, 2] 

since 1 precedes 15, 15 precedes 30, and 2 precedes 6.

Non-Stream Solution

public List<Integer> findSmallPrecedingValues(final List<Integer> values) {      List<Integer> result = new ArrayList<Integer>();     for (int i = 0; i < values.size(); i++) {         Integer next = (i + 1 < values.size() ? values.get(i + 1) : -1);         Integer current = values.get(i);         if (current < next) {             result.push(current);         }     }     return result; } 

What I've Tried

The problem I have is I can't figure out how to access next in the lambda.

return values.stream().filter(v -> v < next).collect(Collectors.toList()); 

Question

  • Is it possible to retrieve the next value in a stream?
  • Should I be using map and mapping to a Pair in order to access next?
like image 281
Pete Avatar asked May 07 '15 00:05

Pete


People also ask

How do you find the max number of a list in a stream?

Calling stream() method on the list to get a stream of values from the list. Calling mapToInt(value -> value) on the stream to get an Integer Stream. Calling max() method on the stream to get the max value. Calling orElseThrow() to throw an exception if no value is received from max()


Video Answer


1 Answers

Using IntStream.range:

static List<Integer> findSmallPrecedingValues(List<Integer> values) {     return IntStream.range(0, values.size() - 1)         .filter(i -> values.get(i) < values.get(i + 1))         .mapToObj(values::get)         .collect(Collectors.toList()); } 

It's certainly nicer than an imperative solution with a large loop, but still a bit meh as far as the goal of "using a stream" in an idiomatic way.

Is it possible to retrieve the next value in a stream?

Nope, not really. The best cite I know of for that is in the java.util.stream package description:

The elements of a stream are only visited once during the life of a stream. Like an Iterator, a new stream must be generated to revisit the same elements of the source.

(Retrieving elements besides the current element being operated on would imply they could be visited more than once.)

We could also technically do it in a couple other ways:

  • Statefully (very meh).
  • Using a stream's iterator is technically still using the stream.
like image 103
Radiodef Avatar answered Sep 21 '22 16:09

Radiodef