Thursday 10 August 2017

How to use Java 8 streams reduce method to concatenate the elements | Java 8 streams


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

StreamDemo.java
import java.util.Arrays;

/**
 * If you want to perform more complex reduction operations, however,
 * you must use the reduce method.
 * 
 * The reduce method expects two arguments: an identity element, and a
 * lambda expression. You can think of the identity element as an
 * element which does not alter the result of the reduction operation. 
 * 
 */
public class StreamDemo
{
    public static void main(String[] args)
    {
        String[] strArray ={ "Welcome ", "to ", "India ", "Peter" };

        /*
         * Reduce method to concatenate all the elements.
         */
        String result = Arrays.stream(strArray).reduce("",(a, b) -> a + b);

        System.out.println(result);
    }
}
Output
Welcome to India Peter

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_reduce_concatinate_App.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/ed01c4e942c222c96b1953312e7a54d5a8556409/BasicJava/StreamDemo_reduce_concatinate_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