Sunday, 20 August 2017

Java 8 Stream Terminal operations examples | Streams in Java 8


Click here to watch in Youtube :
https://www.youtube.com/watch?v=HHdtl6fOjCM&list=UUhwKlOVR041tngjerWxVccw

StreamDemo1.java
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Terminal operations return a result of a certain type instead of
 * again a Stream.
 *
 */
public class StreamDemo1
{
    public static void main(String[] args)
    {
        List<String> nameList = new ArrayList<>();
        nameList.add("Amitabh");
        nameList.add("Shekhar");
        nameList.add("Aman");
        nameList.add("Rahul");
        nameList.add("Shahrukh");
        nameList.add("Salman");
        nameList.add("Yana");
        nameList.add("Lokesh");

        /*
         * This method helps in iterating over all elements of a
         * stream and perform some operation on each of them. The
         * operation is passed as lambda expression parameter.
         */
        nameList.forEach(System.out::println);
    }
}
Output
Amitabh
Shekhar
Aman
Rahul
Shahrukh
Salman
Yana
Lokesh
StreamDemo2.java
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * Terminal operations return a result of a certain type instead of
 * again a Stream.
 *
 */
public class StreamDemo2
{
    public static void main(String[] args)
    {
        List<String> nameList = new ArrayList<>();
        nameList.add("Amitabh");
        nameList.add("Shekhar");
        nameList.add("Aman");
        nameList.add("Rahul");
        nameList.add("Shahrukh");
        nameList.add("Salman");
        nameList.add("Yana");
        nameList.add("Lokesh");

        /*
         * collect() method used to receive elements from a stream and
         * store them in a collection.
         */
        List<String> upperCaseNameList = nameList.stream()
                                                 .sorted()
                                                 .map(String::toUpperCase)
                                                 .collect(Collectors.toList());

        System.out.print(upperCaseNameList);
    }
}
Output
[AMAN, AMITABH, LOKESH, RAHUL, SALMAN, SHAHRUKH, SHEKHAR, YANA]
StreamDemo3.java
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Terminal operations return a result of a certain type instead of
 * again a Stream.
 *
 */
public class StreamDemo3
{
    public static void main(String[] args)
    {
        List<String> nameList = new ArrayList<>();
        nameList.add("Amitabh");
        nameList.add("Shekhar");
        nameList.add("Aman");
        nameList.add("Rahul");
        nameList.add("Shahrukh");
        nameList.add("Salman");
        nameList.add("Yana");
        nameList.add("Lokesh");

        /*
         * Various matching operations can be used to check whether a
         * certain predicate matches the stream. All of those
         * operations are terminal and return a boolean result.
         */

        boolean matchedResult = nameList.stream()
                                        .anyMatch((s) -> s.startsWith("A"));

        System.out.println(matchedResult);

        matchedResult = nameList.stream()
                                .allMatch((s) -> s.startsWith("A"));

        System.out.println(matchedResult);

        matchedResult = nameList.stream()
                                .noneMatch((s) -> s.startsWith("A"));

        System.out.println(matchedResult);
    }
}
Output
true
false
false
StreamDemo4.java
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Terminal operations return a result of a certain type instead of
 * again a Stream.
 *
 */
public class StreamDemo4
{
    public static void main(String[] args)
    {
        List<String> nameList = new ArrayList<>();
        nameList.add("Amitabh");
        nameList.add("Shekhar");
        nameList.add("Aman");
        nameList.add("Rahul");
        nameList.add("Shahrukh");
        nameList.add("Salman");
        nameList.add("Yana");
        nameList.add("Lokesh");

        /*
         * Count is a terminal operation returning the number of
         * elements in the stream as a long.
         */

        long totalMatched = nameList.stream()
                                    .filter((s) -> s.startsWith("A"))
                                    .count();

        System.out.println(totalMatched);
    }
}
Output
2
StreamDemo5.java
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 
 * Terminal operations return a result of a certain type instead of
 * again a Stream.
 *
 */
public class StreamDemo5
{
    public static void main(String[] args)
    {
        List<String> nameList = new ArrayList<>();
        nameList.add("Amitabh");
        nameList.add("Shekhar");
        nameList.add("Aman");
        nameList.add("Rahul");
        nameList.add("Shahrukh");
        nameList.add("Salman");
        nameList.add("Yana");
        nameList.add("Lokesh");

        /*
         * reduce() terminal operation performs a reduction on the
         * elements of the stream with the given function. The result
         * is an Optional holding the reduced value.
         */

        Optional<String> optional = nameList.stream()
                                            .reduce((s1, s2) -> s1 + "#" + s2);

        optional.ifPresent(System.out::println);
    }
}
Output
Amitabh#Shekhar#Aman#Rahul#Shahrukh#Salman#Yana#Lokesh
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_terminal_op_examples_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StreamDemo_terminal_op_examples_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/bc0205890e206d66c03b068b168e05c78f68f50d/BasicJava/StreamDemo_terminal_op_examples_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment