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

后端 未结 6 906
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 18:57

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

6条回答
  •  情深已故
    2020-12-29 19:45

    If you're willing to use a third party library and don't need parallelism, then jOOλ offers SQL-style window functions as follows

    System.out.println(
    Seq.of(10, 1, 15, 30, 2, 6)
       .window()
       .filter(w -> w.lead().isPresent() && w.value() < w.lead().get())
       .map(w -> w.value())
       .toList()
    );
    

    Yielding

    [1, 15, 2]
    

    The lead() function accesses the next value in traversal order from the window.

    Disclaimer: I work for the company behind jOOλ

提交回复
热议问题