I read this Java 8 official docs:
Streams may or may not have a defined encounter order. Whether or not a stream has an encounter order depends on the source and the intermediate operations. Certain stream sources (such as List or arrays) are intrinsically ordered, whereas others (such as HashSet) are not.
If a stream is ordered, repeated execution of identical stream pipelines on an identical source will produce an identical result; if it is not ordered, repeated execution might produce different results.
Tried to understand the mentioned behaviour through this code
public class StreamOrderValidator
{
public static void main( String[] args )
{
String[] colors=new String[] {"red","green","blue","orange"};
List<String> colorsList=Arrays.asList(colors);
HashSet<String> colorsSet=new HashSet<>();
colorsSet.addAll(colorsList);
System.out.println(colorsSet); // [red, orange, green, blue]
List<String> processedColorsSet = processStream(colorsSet.stream());
System.out.println(processedColorsSet); // [RED, ORANGE, GREEN, BLUE]
}
private static List<String> processStream(Stream<String> colorStream) {
List<String> processedColorsList = colorStream.filter(s->s.length()<=6).
map(String::toUpperCase).collect(Collectors.toList());
return processedColorsList;
}
}
I ran this code many a times and the order of elements in resulting stream was always the same (shown as comment). I am not able to figure it out how this justifies the above quoted text about "Order not being preserved for an unordered collection".
I am definitely misunderstanding the extracted text from javadocs.