Its an layer / wrapper around collection, that enables declarative data processing like SQL. They can be sourced from Collections , Arrays . I/O.
Some characteristics include
Streams have 2 kinds of operations to it
With these inputs its easy to see how these code snippets would work
Another Example
Streams can be of two types
Some characteristics include
- Support pipeline operations : output of a stream operation can be another stream
- Auto Iterations
- Lazy Evaluation
- Automatic parallelization
Streams have 2 kinds of operations to it
- Intermediate : they return one more stream
- filter
- map
- sorted
- Terminal : they return a void or a non-stream
- forEach
- ifPresent
With these inputs its easy to see how these code snippets would work
public static void method1() { Arrays.asList("alpha1", "alpha2", "alpha3") .stream() .findFirst() .ifPresent(System.out::println); }
Another Example
public static void method2() { List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList()); System.out.println(filtered); }
Streams can be of two types
- Sequential
- Parallel : can be executed parallely without need to write MultiThreaded code.
No comments:
Post a Comment