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 - 671869652
2 - 1199998894
1 - 434710057
2 - 1066276340
1 - 1210324379
2 - 1005342918
1 - 1801773564
2 - 613054673
1 - 1282025750
2 - 358169149
-- distinct ordered --
1 - 671869652 | 2 - 1199998894 |
1 - 671869652 | 2 - 1199998894 |
1 - 671869652 | 2 - 1199998894 |
1 - 671869652 | 2 - 1199998894 |
1 - 671869652 | 2 - 1199998894 |
-- distinct unordered --
1 - 1801773564 | 2 - 613054673 |
1 - 1801773564 | 2 - 1005342918 |
1 - 1801773564 | 2 - 1005342918 |
1 - 1801773564 | 2 - 1005342918 |
1 - 1801773564 | 2 - 1005342918 |