简体   繁体   English

如何保持流中元素的顺序

[英]How to preserve order of the elements in streams

I want to preserve the order of array elements using below spliterator() method. 我想使用以下spliterator()方法保留数组元素的顺序。

However, when I run this program it is producing 4 2 1 3 as output. 但是,当我运行此程序时,它将产生4 2 1 3作为输出。

   public class Test1 {
    public static void main(String[] args) {
        m1().anyMatch(e -> {
            System.out.println(e);
            return false;
        });
    }

    private static LongStream m1() {
    return StreamSupport.longStream(Arrays.spliterator(new long[] { 1, 2, 3, 4 }), true);
    }
}

Kindly check and let me know if this thing is possible or not. 请检查并让我知道这件事是否可行。

You are requesting a parallel stream, hence, you can't get a defined processing order , as parallel processing and ordered processing are a general contradiction. 您正在请求并行流,因此,您无法获得已定义的处理顺序 ,因为并行处理和有序处理是一个普遍的矛盾。

If you want the elements to be processed in order, you have to do both, request a sequential stream and use a terminal operation that depends on the order: 如果要按顺序处理元素,则必须同时执行这两项操作,请求顺序流并使用取决于顺序的终端操作:

public static void main(String[] args) {
    m1().filter(e -> {
        System.out.println(e);
        return false;
    }).findFirst();
}

private static LongStream m1() {
    return StreamSupport.longStream(Arrays.spliterator(new long[] { 1, 2, 3, 4 }), false);
}        

But note that you should avoid writing software which relies on the processing order . 但是请注意,您应该避免编写依赖于处理顺序的软件。 You should define your task in a way that the processing steps do not depend on the processing order, even if the final result may depend on the encounter order . 即使最终结果可能取决于遇到的顺序 ,也应该以不依赖处理顺序的方式定义任务。

Eg, if you write 例如,如果您写

public static void main(String[] args) {
    m1().filter(e -> {
        System.out.println(e);
        return e%2 == 0;
    })
    .findFirst()
    .ifPresent(result -> System.out.println("final result: "+result));
}

private static LongStream m1() {
    // note that you can have it much simpler:
    return LongStream.of(1, 2, 3, 4 ).parallel();
}        

the intermediate output may be in arbitrary order and there's no guaranty whether or not you will see “3” and “4” in the output, but you have the guaranty that the last output will be “final result: 2”, as that's the first matching element in encounter order . 中间输出可以是任意顺序,并且无法保证输出中是否显示“ 3”和“ 4”,但是您可以保证最后一个输出将是“最终结果:2”,因为这是遇到顺序中的第一个匹配元素。 See also here . 另请参阅此处

Well you are using anyMatch which is not preserving order in the first place, so thats pretty much expected. 好吧,您使用的是anyMatch ,它首先不保留顺序,因此,这非常值得期待。 If you want to get the first element use findFirst for example. 如果要获取第一个元素,请使用findFirst例如。

As you are creating a parallel stream (second parameter = true ) StreamSupport.longStream(Arrays.spliterator(new long[] { 1, 2, 3, 4 }), true) you don't get the ordered access. 在创建并行流(第二个参数= true )时, StreamSupport.longStream(Arrays.spliterator(new long[] { 1, 2, 3, 4 }), true)您不会获得有序访问。 If you create a sequential stream (change second parameter from true to false ) you should get what you want. 如果创建顺序流(将第二个参数从true更改为false ),则应该获得所需的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Streams - 如何在使用 Collector groupingBy() 时保留元素的初始顺序 - Java Streams - How to preserve the initial Order of elements while using Collector groupingBy() partitioningBy是否保留原始列表中元素的顺序? - Does partitioningBy preserve order of elements in the original list? 如何在TestNG中保留组顺序 - How to preserve group order in TestNG 如何从两个流中获取相似的元素并收集这样形成的对而不会丢失顺序? - How to get similar elements from two streams and collect the pairs thus formed without losing the order? Java 8 Streams - 如何比较元素? - Java 8 Streams - how to compare elements? 在插入排序算法中,相等元素是否保留了它们的顺序? - Do equal elements preserve their order in Insertion Sort algorithm? 保留Neo4j的数据成员中元素的插入顺序 - Preserve insertion order of elements in a data member for Neo4j Jsoup拆分块元素以保留文本和子元素的顺序 - Jsoup split block element to preserve order of text and child elements 如何使用 CyclingBarrier 同步线程并保留它们的执行顺序? - How to synchronise threads and preserve their execution order with CyclingBarrier? 如何在Java中使用EnumMap保留订单 - How can I preserve order with an EnumMap in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM