STREAM API in Java

The STREAM API is known in computing as one of the best and a part of Java’s eight(8) functional programming model. In addition, the syntax allows users to process data in the best declarative manner.

Another descriptive feature of Stream API is that it also provides users with a way to process data in a pipeline fashion, where a user can have access to chain multiple operations together to perform such complex data processing tasks.

In addition, to those above, a stream is known to be a sequence of elements that can be processed in a known pipeline. however, a user can create a Stream from different data sources such as collections, arrays or even I/O channels.

Methods for processing data

  • The filter command, that is filter( )
  • map( )
  • reduce ( )
  • collect ( )

Practical Approach:

The raw code takes only 6 lines of syntaxes to allocate or filter and sum a list of numbers.

1                    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

2       int sum = numbers.stream()

 3                   .filter(n -> n % 2 == 0) // filter even numbers

  4             .mapToInt(n -> n) // convert to int

 5       .sum(); // sum the numbers

6     System.out.println(sum); // prints 6

 

Note that the code given above computes and calculates the sum of the given numbers in a list using Java8’s Stream API.

Related posts:

About Author


Discover more from SURFCLOUD TECHNOLOGY

Subscribe to get the latest posts sent to your email.

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from SURFCLOUD TECHNOLOGY

Subscribe now to keep reading and get access to the full archive.

Continue reading