搜索不一致的行为java-8流的示例? [英] search for example of inconsistent behavior java-8 stream?

查看:139
本文介绍了搜索不一致的行为java-8流的示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java 8文档中( doc order stream ),可以看到:


如果[流]没有排序,重复执行可能会产生不同的
结果。


我的问题很简单:是否有一个简单的方法可以用一个小单位来说明这个事实测试(也许有一个HashMap或类似的东西)?
$ b 整个报价在这里:


对于顺序流,遇到命令的存在与否不会影响性能,只会影响确定性。如果一个流被排序,那么在相同的源上重复执行相同的流管线将产生相同的结果;如果未订购,重复执行可能会产生不同的结果。


因此,我的问题是关于严格顺序而非并行执行。这是我的疑问。

解决方案

显而易见的答案是,只要您使用无序你应该得到不同的结果。例如使用这个:

  int first = Arrays.asList(1,2,3,4).stream()
.unordered()
.parallel()
.findFirst()
.get();
System.out.println(first);

应该产生一个 not 总是为1的结果。因为流是无序,所以任何结果 [1,2,3,4] 都是可能的。



8这不是真的,流管道不考虑 unordered

  @Override 
public< P_IN> O evaluateParallel(PipelineHelper< T> helper,
Spliterator< P_IN>分隔符){
返回新的FindTask<>(this,helper,spliterator).invoke();
}

但java-9中的内容有所变化:

  @Override 
public< P_IN> O evaluateParallel(PipelineHelper< T> helper,
Spliterator< P_IN>分隔符){
//这考虑到上游操作标志和终端
// op标志,因此考虑到findFirst或findAny
布尔mustFindFirst = StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags());
返回新的FindTask<>(this,mustFindFirst,helper,spliterator).invoke();



$ b

因此,在java-9下多次运行相同的代码会产生不同的结果。

有些操作已经是无序,比如 Stream#generate Stream#forEach

In java 8 documentation (doc order stream), one can see this :

if [a stream] is not ordered, repeated execution might produce different results.

my question is quite simple : is there an easy way to illustrate this fact in a little unit test (maybe with an HashMap or some thing like this) ?

[Edit] the whole quote is here :

For sequential streams, the presence or absence of an encounter order does not affect performance, only determinism. If a stream is ordered, repeated execution of identical stream pipelines on an identical source will produce an identical result; if it is not ordered, repeated execution might produce different results.

Thus, my question is about a strictly sequential not parallele execution. It is this case that I'm questioning about.

解决方案

The obvious answer is that whenever you use unordered you should get different results. For example using this:

int first = Arrays.asList(1, 2, 3, 4).stream()
           .unordered()
           .parallel()
           .findFirst()
           .get();
System.out.println(first);

should produce a result that is not always 1. Because the stream is unordered, so any result out of [1,2,3,4] is possible.

In java-8 this is not true, the stream pipeline does not take that unordered into account:

    @Override
    public <P_IN> O evaluateParallel(PipelineHelper<T> helper,
                                     Spliterator<P_IN> spliterator) {
        return new FindTask<>(this, helper, spliterator).invoke();
    }

But things have change in java-9:

    @Override
    public <P_IN> O evaluateParallel(PipelineHelper<T> helper,
                                     Spliterator<P_IN> spliterator) {
        // This takes into account the upstream ops flags and the terminal
        // op flags and therefore takes into account findFirst or findAny
        boolean mustFindFirst = StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags());
        return new FindTask<>(this, mustFindFirst, helper, spliterator).invoke();
    }

So running the same code under java-9 multiple times will produce a different result.

There are operations that are already unordered like Stream#generate and Stream#forEach.

这篇关于搜索不一致的行为java-8流的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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