Thursday 24 August 2017

Terminal operations[foreach and toArray] in Java 8 Stream | Streams in Java 8


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

Click the below Image to Enlarge
Terminal operations[foreach and toArray] in Java 8 Stream | Streams in Java 8 
StreamForEachDemo.java
import java.util.stream.Stream;

public class StreamForEachDemo
{
    public static void main(String[] args)
    {
        
        Stream<String> stream = Stream.of("peter", "Juli", "David");
        /*
         * iterates over each element using forEach:
         */
        stream.forEach(System.out::println);

    }
}
Output
peter
Juli
David
StreamToArrayDemo.java
import java.util.stream.Stream;

public class StreamToArrayDemo
{
    public static void main(String[] args)
    {

        Object[] objectArray = Stream.of(1, 2, 3, 4, 5).toArray();
        
        for (Object object : objectArray)
        {
            System.out.println(object);
        }

        System.out.println("-----------------------------------");

        String[] upperCaseNameArray = (String[]) Stream.of("Peter", "Juli", "Ram")
                                                       .map(s -> s.toUpperCase())
                                                       .toArray(String[]::new);

        for (String name : upperCaseNameArray)
        {
            System.out.println(name);
        }

    }
}
Output
1
2
3
4
5
-----------------------------------
PETER
JULI
RAM
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_terOp_fe_toArray_App.zip?attredirects=0&d=1

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

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