如何使用Java 8流来查找更大值之前的所有值? [英] How to use Java 8 streams to find all values preceding a larger value?

查看:154
本文介绍了如何使用Java 8流来查找更大值之前的所有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Katas在工作中发布的一些编码,我偶然发现了这个问题,我不知道如何解决。

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


使用Java 8 Streams,给定正整数列表,生成一个
的整数列表,其中整数前面有一个更大的值。

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]

上述输入将产生:

[1, 15, 2]

因为1在15之前,15在30之前,2在6之前。

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;
}



我尝试过什么



我遇到的问题是我无法弄清楚如何在lambda中访问下一个。

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());



问题




  • 是否可以检索流中的下一个值?

  • 我应该使用 map 并映射到配对以便访问下一个?

  • 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?
    • 推荐答案

      使用 IntStream。范围

      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?

      不,不是真的。我知道的最好的引用是在 java.util.stream 包描述

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


      元素流的生命周期中只访问过一次。像 Iterator 一样,必须生成一个新流来重新访问源的相同元素。

      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:


      • 有条不紊地(非常meh)。

      • 使用流的 iterator 技术上仍在使用流。

      • Statefully (very meh).
      • Using a stream's iterator is technically still using the stream.

      这篇关于如何使用Java 8流来查找更大值之前的所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆