Ordered Streams vs. unordered Streams
Description
A stream can be ordered or unordered.
An ordered stream keeps the order of its elements.
The Streams API can convert an ordered stream, which may represent an ordered data source such as a list or a sorted set, into an unordered stream.
We can also convert an unordered stream into an ordered stream by applying an sorting intermediate operation.
Example
import java.util.Arrays;
import java.util.List;
//w w w . j a v a2s . c o m
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(3,7,9,3,1,2,1, 2, 3, 4, 5);
numbers.stream()
.filter(n -> n % 2 == 1)
.sorted()
.forEach(System.out::println);
}
}
The code above generates the following result.