Stream in Java8
Stream is the enhancement of Collection package, it focuses on providing convenient aggregate operation for the elements in collection. So it is not a datatype or anything, it is about algorithms, it’s like an advanced Iterator. The original iterator can only provide the iterate operation, but stream, user can give instructions on what to do on the elements of collections.
So the stream is like an iterator, single direction and can’t push back. Like a stream.
Example:
@Test
public void lenStream() {
List list = Arrays.asList(“java”, “scala”, “python”, “shell”, “ruby”);
long num = list.parallelStream().filter(x -> x.length() < 5).count();
System.out.println(num);
}
The difference between stream and collection:
- Stream did not store any data like collections do.
- Stream will not modify the original data source, it will always come up with a new data

浙公网安备 33010602011771号