Skip to content

5.22 Self 9.3

What is Stream?

  • We can consider Stream as a pipeline, through which our collection elements passes through.
  • While elements passes through pipelines, it perform various operations like sorting, filtering etc.
  • Useful when deals with bulk processing. (can do parallel processing)

  • Collection (Start)

  • Step 1: Create Stream

    • At Step 1: Streams are created from the data source like collection or array etc.
    • Step 2: Intermediate Operations

    • At Step 2:

    • Intermediate Operations like:

      • filter()​,
      • sorted()​,
      • map()​,
      • distinct()​ etc... are used.
        • These operations transform the stream into another stream and more operations can be done on top of it.
        • These are Lazy in nature, means these operations get executed only when terminal operation is invoked.
      • Step 3: Terminal Operations
    • At Step 3:

    • Terminal Operations like:

      • collect()​,
      • reduce()​,
      • count()​ etc... are used.
        • These operations trigger the processing of the stream.
        • And produce the Output. Means after Terminal operation used, no more operation we can perform.

An easy example

For Example:

public class StreamExample {

    public static void main(String args[]) {

        List<Integer> salaryList = new ArrayList<>();
        salaryList.add(3000);
        salaryList.add(4100);
        salaryList.add(9000);
        salaryList.add(1000);
        salaryList.add(3500);

        int count = 0;
        for (Integer salary : salaryList) {
            if (salary > 3000) {
                count++;
            }
        }

        System.out.println("Total Employee with salary > 3000: " + count);
    }
}

using Stream

public class StreamExample {

    public static void main(String args[]) {

        List<Integer> salaryList = new ArrayList<>();
        salaryList.add(3000);
        salaryList.add(4100);
        salaryList.add(9000);
        salaryList.add(1000);
        salaryList.add(3500);

        long output = salaryList.stream().filter((Integer sal) -> sal > 3000).count();

        System.out.println("Total Employee with salary > 3000: " + output);
    }
}

Output:

Total Employee with salary > 3000: 3

Different ways to create a stream:

  1. From Collection:
List<Integer> salaryList = Arrays.asList(3000, 4100, 9000, 1000, 3500);
Stream<Integer> streamFromIntegerList = salaryList.stream();
  1. From Array:
Integer[] salaryArray = {3000, 4100, 9000, 1000, 3500};
Stream<Integer> streamFromIntegerArray = Arrays.stream(salaryArray);
  1. From Static Method:
Stream<Integer> streamFromStaticMethod = Stream.of(1000, 3500, 4000, 9000);
  1. From Stream Builder:
Stream.Builder<Integer> streamBuilder = Stream.builder();
streamBuilder.add(1000).add(9000).add(3500);

Stream<Integer> streamFromStreamBuilder = streamBuilder.build();
  1. From Stream Iterate:
Stream<Integer> streamFromIterate = Stream.iterate(seed: 1000, (Integer n) -> n + 5000).limit(maxSize: 5);

Different Intermediate Operations

We can chain multiple Intermediate operations together to perform more complex processing before applying terminal operation to produce the result.

image

Using predicate to process name, and return a bool. If the result is true, then accept this name to the stream

After this, we need a terminal operator collect

image

image

image

image

image

image

image

image

Different Terminal Operator

image

image

image

image

image

image

image

image

image

image

image

image