For unordered stream (having no encounter order), the result is still the same, that's because unordered() method does not actually unordered or shuffle the elements but it just removes the ordered characteristics from the stream which improves the performance in case of parallel processing.
package com.logicbig.example.intstream;
import java.util.stream.IntStream;
public class FindFirstExample2 {
public static void main(String... args) {
IntStream intStream = IntStream.of(1, 2, 3, 2, 5, 4);
int i = intStream.unordered()
.findFirst()
.orElse(-1);
System.out.println(i);
//parallel
IntStream intStream2 = IntStream.of(1, 2, 3, 2, 5, 4);
int i2 = intStream2.unordered()
.parallel()
.findFirst()
.orElse(-1);
System.out.println(i2);
}
}
Output
1
1