Java 8 Stream Operations with examples

Java 8 streams consist of both Intermediate and Terminal operations.
 

Intermediate Operations

 
These operations return a stream.
 
These are operations to be performed to transform the data like filtering or sorting operations.

 

Terminal Operations

 
These operations describe what to do with the processed data.
 
They return a value, not a stream.
 
Only one terminal operation can be performed per stream.
 
For example, count(), collect() are terminal operations.
 
 

Intermediate and Terminal operations with examples

 

Example : (Filtering countries by name starting with A)

 

fiter() method

 
This method accepts a predicate and filters the stream based on the condition provided in the predicate.
 
We can also use a lambda expression in place of a predicate.
 
Since it returns a stream, we need a terminal operation like count() or collect() to process it.
 
In below example, the filter() method acts on the stream of country names and only chooses the names starting with “A”.
 
stream filter operation

This stream is then used by the count() method to count the strings.
 

package com.topjavatutorial.java8examples;

import java.util.Arrays;
import java.util.List;

public class StreamOperations {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    List<String> countries = Arrays.asList("Australia","Brazil", "China","Denmark","England","France","Germany");
    
    //Count country names starting with A
    
    long count = countries.stream().filter(countryName -> countryName.startsWith("A")).count();
    
    System.out.println("Country names starting with A = " + count);

  }

}


 
Running this program will produce output :

Country names starting with A = 1
 

count() method

 
This method is a terminal operation and it return a long value for the count of elements in the stream.
 
In the above example, count() method count the number of strings in the stream returned by the filter() method.

 
 

map() method

 
This method is useful to map the elements of one stream to another.
 
stream map operation
For example, the following map() operation converts each string to uppercase.
 

package com.topjavatutorial.java8examples;

import java.util.Arrays;
import java.util.List;

public class StreamOperations {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    List<String> countries = Arrays.asList("Australia","Brazil", "China","Denmark","England","France","Germany");
    
    
    countries.stream().map(name -> name.toUpperCase())
              .forEach(System.out::println);
    

  }

}


 
Running this program will print the country names in uppercase.
 
AUSTRALIA
BRAZIL
CHINA
DENMARK
ENGLAND
FRANCE
GERMANY

 
 

forEach() method

 
This method takes a lambda expression and applies that expression for each element of the stream.
 
It does not return any value, just performs the operation.

In the previous example, the forEach() method just prints all the country names in the stream.
 
 

collect() method

 
This method collects elements from a stream and stores them in a collection.
 
In the below example, map() method converts each name to uppercase and then collect() method collects the uppercase names in a list.
 

package com.topjavatutorial.java8examples;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamOperations {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    List<String> countries = Arrays.asList("Australia","Brazil", "China","Denmark","England","France","Germany");
    
    countries.stream().map(name -> name.toUpperCase())
              .collect(Collectors.toList())
              .forEach(System.out::println);
    

  }

}


 
Running this program will print the country names in uppercase.
 
AUSTRALIA
BRAZIL
CHINA
DENMARK
ENGLAND
FRANCE
GERMANY
 
 

sorted() method

 
This method sorts the elements in ascending order and returns the sorted stream.
 
In the below example, the elements of lost are sorted using sorted() and then printed with forEach().
 

package com.topjavatutorial.java8examples;

import java.util.Arrays;
import java.util.List;

public class StreamOperations {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    List<String> countries = Arrays.asList("Germany","England", "China","Denmark","Brazil","France","Australia");
    
    
  countries.stream().sorted().forEach(System.out::println);
    

  }

}


 
Running this program will print the country names sorted in ascending order.
 
Australia
Brazil
China
Denmark
England
France
Germany

 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

You may also like...

2 Responses

  1. January 10, 2016

    […] refer following post for stream operations:   Stream Operations explained with examplesStream Operations explained with examples   […]

  2. January 17, 2016

    […] Streams Java 8 Stream Operations with Examples   […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: