1the Stream API is used to process collections
2of objects. A stream is a sequence of objects
3that supports various methods which can be
4pipelined to produce the desired result.
5 It is very similar to build().perform()
6 method in Actions class. It is chaining
7 some operations in an order.
8
9The features of Java stream are:
10- A stream is not a data structure instead
11it takes input from the Collections,
12Arrays or I/O channels.
13- Streams don’t change the original data
14structure, they only provide the result
15as per the pipelined methods.
16- Each intermediate operation is lazily
17executed and returns a stream as a result,
18hence various intermediate operations
19can be pipelined. Terminal operations mark
20the end of the stream and return the result.
21
22Different Operations On Streams:
23
24map ==> The map method is used to return a
25stream consisting of the results of applying
26the given function to the elements of this stream.
27List number = Arrays.asList(2,3,4,5);
28List square = number.stream().map(x->x*x).collect(Collectors.toList());
29
30filter ==> The filter method is used to
31select elements as per the Predicate passed as argument.
32List names = Arrays.asList("Reflection","Collection","Stream");
33List result = names.stream().filter(s->s.startsWith("S")).collect(Collectors.toList());
34
35sorted ==> The sorted method is used to sort the stream.
36List names = Arrays.asList("Reflection","Collection","Stream");
37List result = names.stream().sorted().collect(Collectors.toList());
38
39ArrayList<String> list3 = new ArrayList(Arrays.asList("monkey", "donkey","onion"));
40List<String> list4 = list3.stream().filter(each->!"onion".equals(each)).collect(Collectors.toList());
41System.out.println(list4);
42
43static String reverse(String word){
44 return Arrays.stream(word.split("")).reduce("", (x,y) -> y+x );
45}
1public class StreamBuilders
2{
3 public static void main(String[] args)
4 {
5 Stream<Integer> stream = Stream.of( new Integer[]{1,2,3,4,5,6,7,8,9} );
6 stream.forEach(p -> System.out.println(p));
7 }
8}
9
1Arrays.asList("a1", "a2", "a3")
2 .stream()
3 .findFirst()
4 .ifPresent(System.out::println); // a1
5
1// Creates a list of ProgrammingLanguage objects,
2// maps it with a method of ProgrammingLanguage to get the name
3// and filters them for programming languages, which are not starting with C.
4
5
6import java.util.List;
7import java.util.Arrays;
8import java.util.stream.Collectors;
9
10
11class ProgrammingLanguage {
12
13 private String name = "";
14
15 public static void main(final String args[]) {
16 List<ProgrammingLanguage> programmingLanguages = Arrays.asList(
17 new ProgrammingLanguage("Java"),
18 new ProgrammingLanguage("Python"),
19 new ProgrammingLanguage("C#"),
20 new ProgrammingLanguage("JS"),
21 new ProgrammingLanguage("C++"),
22 new ProgrammingLanguage("C"));
23 List<String> nonCProgrammingLanguages = programmingLanguages.stream() // creates stream from list
24 .map(ProgrammingLanguage::getName)
25 .filter(name -> !name.startsWith("C"))
26 .collect(Collectors.toList()); // convert stream back to list
27 System.out.println("Names not starting with C: " + nonCProgrammingLanguages);
28 }
29
30
31 public ProgrammingLanguage(final String name) {
32 setName(name);
33 }
34
35 public String getName() {
36 return name;
37 }
38
39 public void setName(final String name) {
40 this.name = name;
41 }
42}
1
2package com.mkyong.java8;
3
4import java.util.stream.IntStream;
5
6public class ParallelExample3a {
7
8 public static void main(String[] args) {
9
10 System.out.println("Normal...");
11
12 IntStream range = IntStream.rangeClosed(1, 10);
13 System.out.println(range.isParallel()); // false
14 range.forEach(System.out::println);
15
16 System.out.println("Parallel...");
17
18 IntStream range2 = IntStream.rangeClosed(1, 10);
19 IntStream range2Parallel = range2.parallel();
20 System.out.println(range2Parallel.isParallel()); // true
21 range2Parallel.forEach(System.out::println);
22
23 }
24
25}
26