Java 8 Streams API 函数

huangapple go评论71阅读模式
英文:

Java 8 Streams API functions

问题

我对Java 8的流(Streams) API还不熟悉。我在使用forEachfilter时注意到以下这段代码可以通过两者中的任一个进行修改以产生相同的结果。在这种情况下,这两种方法有何不同,如何决定何时使用哪种方法?

items.forEach(item -> {
    if (item.contains("B")) {
        System.out.println(item);
    }
});
items
    .filter(item -> item.contains("B"))
    .forEach(System.out::println);
英文:

I am new to Java 8 Streams API. I was working with forEach and filter and noticed that the following piece of code can be modified with either to produce the same result. In that case, how are the two different and how to decide when to use which?

items.forEach(item -> {
    if (item.contains("B") {
        System.out.println(item);
    }
});

<!-- -->

items
    .filter(item -&gt; item.contains(&quot;B&quot;))
    .forEach(System.out::println);

答案1

得分: 1

他们有相同的效果。

区别在于调用 filter 的代码部分使用了流的过滤操作,而带有 if 语句的 forEach 使用了普通的 if 语句。

使用哪种取决于您想应用的风格,但是函数式编程范式,也就是后一个代码片段的风格,最近变得更受欢迎。

我个人会使用

items
    .filter(item -> item.contains("B"))
    .forEach(System.out::println);

因为我认为这样更易读。

英文:

They have the same effect.

The difference is that the piece of code calling filter uses the stream's filtering operation, whereas forEach with the if statement uses a plain old if statement.

Which one to use depends on what style you want to apply, but the functional programming paradigm, the style of the latter piece of code, has become more popular recently.


I personally would use

items
    .filter(item -&gt; item.contains(&quot;B&quot;))
    .forEach(System.out::println);

as I find this much better readable.

答案2

得分: 0

除了可读性外:对于第二个版本,这两个调用可以并行处理,这在两个过滤器和输出都可能需要一些时间的情况下尤其相关。

想象一下,过滤器检查一个数据库,调用需要5秒钟,而且与在控制台上打印不同,你将某些内容上传到一个服务器,这也需要5秒钟。

在你的第一个版本中,流水线在forEach调用中需要花费10秒钟的时间(如果有输出的话)。

在第二个版本中,一个元素经过过滤调用后,另一个元素可以被过滤,同时第一个元素正在输出;这意味着整个过程只需要15秒,而在这种情况下需要20秒。

英文:

Apart from readability: for the second version, the two calls can be parallelized, which is relevant especially if both the filter and the output can take some time.

Imagine the filter checks a database with a call that takes 5 seconds, and instead of printing to the console, you upload something to a server which also takes 5 seconds.

In your first version, the stream pipeline will need to spend 10 seconds within the forEach call (if an output happens, obviously).

In the second version, after one element has passed the filter call, another element can be filtered, while the first is being output; that means that the whole process takes only 15 seconds instead of 20 in this case.

答案3

得分: -1

The grammar is different but the result is the same :)! The forEach() grammar is clear. The filter grammar is hidden by your lambda item -> item.contains("B").
Without the lambda, .filter would take an instance of a 'Predicate' for example:

public class MyPredicate<String> implements Predicate {
  @Override
  public boolean test(String myString) {
    return myString.contains("B");
  }
}

Which you would use like this - .filter(new MyPredicate()).

英文:

The grammar is different but the result is the same :)! The forEach(..) grammar is clear. The filter grammar is hidden by your lambda item -> item.contains("B").
Without the lambda, .filter would take an instance of a 'Predicate' for example:

public Class MyPredicate&lt;String&gt; implements Predicate {
  @Override
  public boolean test(String myString) {
    return myString.contains(&quot;B&quot;);
  }
}

Which you would use like this - .filter(new MyPredicate()).

huangapple
  • 本文由 发表于 2020年3月16日 19:59:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/60705606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定