Sunday, 16 August 2015

Stream API in Java 8

Java 8 Stream API is a new API which got recently introduced in Java Collections. Using Streaming API, Collections can be processed through different APIs. Stream API is introduced to work with LAMBDA Expression.
                         
                                                                     Java 8 Stream is different from Java IO Streaming. IO Stream is a collection of bytes or characters where as Java 8 Stream API is a collection of objects.
Stream Pipeline consists of three types of steps-
  • A source
  • Zero or more intermediate operations
  • A terminal operation
      


Example


int salarySum = employees.stream()
                           .filter(e->e.getCity().equals("A"))
                           .map(i->i.getSalary())
                           .sum();
Table of Contents:

Getting a stream from collection


 

Stream filter

 

Stream map

 

Stream collect

 

Stream max/min

 

Stream count

 

Stream reduce

 

Stream sum






Getting a stream from collection


Suppose your collection looks like:

List collections=new ArrayList();
collections.add("Bangalore");
collections.add("Mumbai");
collections.add("New York");
collections.add("California");
collections.add("san diego");
collections.add("san francisco");


Stream steam=collections.stream();





Stream filter


Filter is boolean condition, it filters based on the condition. It takes Predicate as a function which internally makes a call to test method

steam.filter(s-> s.startsWith("s"));




Stream map


map method updates or modifies each 'n' every element of the collection. In this example, if we want to update all the city's name with lower/upper case, we can achieve it using map method.

 

steam.map(s->s.toUpperCase());




Stream collect


collect is an terminal Operation. This method collects the data from stream. All the intermediate Operations will be executed as soon as terminal operation gets invoked. Collection Object on which we are getting stream, is final.

List newList=collections.stream()
.filter(s-> s.startsWith("s"))
.map(s->s.toUpperCase())
.collect(Collectors.toList());



Stream max/min


max/min method returns the maximum/minimum element from collection based on the Comparison Condition. max/min takes a Comparator as an argument for comparison In this example if we are comparing the cities based on length of names .

String maxCityByLength=collections.stream()
.map(s->s.toUpperCase())
.max((s1,s2)->s1.length()-s2.length())
.get();

String minCityByLength=collections.stream()
.map(s->s.toUpperCase())
.min((s1,s2)->s1.length()-s2.length())
.get();





Stream count


count  returns the number of elements, which satisfies all the intermediate conditions. It is also a Terminal Operation.

long count = collections.stream()
.filter(s-> s.startsWith("s"))
.count(); //2




Stream reduce


The reduce() method reduces/merge the elements of a stream to a single value.

String result=collections.stream()
.reduce((s,k)->s+"-"+k)
.get();

reduce takes Binary Operator as a parameter. It can be passed using LAMBDA Expression.





Stream sum


The sum() is used to add all the Integer Elements.

long count = collections.stream()
.mapToInt(i->i.length())
.sum();



No comments:

Post a Comment