This example shows that unordered stream does not respect the stability. Stream#distinct() method returns the same identical results in both cases but with unordered stream, the corresponding instance might be picked randomly. Using unordered stream improves the performance though.

package com.logicbig.example.stream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class UnorderedExample2 {
public static void main(String... args) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add(new Integer(1));
list.add(new Integer(2));
}
//The original list has 10 different object instances but half of them are identical by values
System.out.println("Original collection with identityHashCodes: ");
list.forEach(i -> System.out.println(i.toString() + " - " + System.identityHashCode(i)));
System.out.println("-- distinct ordered --");
for (int i = 0; i < 5; i++) {//repeat 5 times to check stability
Stream<Integer> stream = list.stream();
Object[] objects = stream.parallel().distinct().toArray();
for (Object object : objects) {
System.out.print(object.toString() + " - " + System.identityHashCode(object) + " | ");
}
System.out.println();
}
System.out.println("-- distinct unordered --");
for (int i = 0; i < 5; i++) {//repeat 5 times to check stability
Stream<Integer> stream = list.stream();
Object[] objects = stream.unordered().parallel().distinct().toArray();
for (Object object : objects) {
System.out.print(object.toString() + " - " + System.identityHashCode(object) + " | ");
}
System.out.println();
}
}
}
Output
Original collection with identityHashCodes:
1 - 1773025027
2 - 841427034
1 - 286092284
2 - 1665258840
1 - 2068836380
2 - 1889628887
1 - 168404404
2 - 1729055416
1 - 1665440085
2 - 403208224
-- distinct ordered --
1 - 1773025027 | 2 - 841427034 |
1 - 1773025027 | 2 - 841427034 |
1 - 1773025027 | 2 - 841427034 |
1 - 1773025027 | 2 - 841427034 |
1 - 1773025027 | 2 - 841427034 |
-- distinct unordered --
1 - 168404404 | 2 - 1889628887 |
1 - 168404404 | 2 - 1889628887 |
1 - 168404404 | 2 - 1889628887 |
1 - 168404404 | 2 - 1889628887 |
1 - 168404404 | 2 - 1889628887 |