2

I want to preserve the order of array elements using below spliterator() method.

However, when I run this program it is producing 4 2 1 3 as output.

   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.

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
T-Bag
  • 10,916
  • 3
  • 54
  • 118

3 Answers3

7

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.

E.g., 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. See also here.

Holger
  • 285,553
  • 42
  • 434
  • 765
2

Well you are using anyMatch which is not preserving order in the first place, so thats pretty much expected. If you want to get the first element use findFirst for example.

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • But even this method does not preserve the order while streaming. I tried `StreamSupport.longStream(Arrays.spliterator(new long[] { 1, 2, 3, 4 }), true).filter(e -> {System.out.println(e); return false;}).findFirst()` and the elements were printed in random order. I think it is the parallel stream that causes this behaviour. findFirst() has to find any and then determine if it is the first element in the Stream. – Madjosz Jul 03 '17 at 10:35
  • 2
    @Madjosz the order is guaranteed for the terminal operation, i.e. try with `forEachOrdered` or collect the elements to a `List` and print those. The *processing order* (in your `filter`) is not specified to exist. – Eugene Jul 03 '17 at 10:42
  • This is right, of course the original order will be preserved if a terminal opeartion is used. There might be a performance impact if you stream parallel and then collect sequential ordered. I don't know anyways why the asker wants to preserve the order while operating in a parallel stream. This would be a very big overhead, since you need some managing process which supplies the elements to the multiple threads in correct order. – Madjosz Jul 03 '17 at 10:56
  • 3
    @Madjosz: preserving the order of the processing in a parallel stream would be more than a big overhead, it would contradict the parallel processing in general. If the processing steps have an order, they aren’t parallel. – Holger Jul 03 '17 at 11:20
1

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. If you create a sequential stream (change second parameter from true to false) you should get what you want.

mark_o
  • 2,052
  • 1
  • 12
  • 18
  • 1
    order does not depend on parallel or sequential, its the operations that define the order... – Eugene Jul 03 '17 at 10:13