Tuesday 29 August 2017

map, mapToInt, mapToDouble and mapToLong method of Java 8 Stream | Streams in Java 8


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

StreamMapDemo.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

public class StreamMapDemo
{
    public static void main(String[] args)
    {
        List<Integer> list = Arrays.asList(1, 2, 3, 4);
        System.out.print("\nStream = ");

        /*
         * Stream.map(): It returns a stream after applying given
         * function to each element of the stream.
         */
        Stream<Integer> stream = list.stream().map(i -> i * i);
        stream.forEach(s -> System.out.print(s + " "));

        System.out.print("\nDoubleStream = ");

        /*
         * mapToDouble(): It returns DoubleStream after applying the
         * given function.
         */
        DoubleStream doubleStream = list.stream().mapToDouble(i -> i * i);
        doubleStream.forEach(s -> System.out.print(s + " "));

        System.out.print("\nLongStream = ");

        /*
         * mapToLong(): It returns LongStream after applying the given
         * function.
         */
        LongStream longStream = list.stream().mapToLong(i -> i * i);
        longStream.forEach(s -> System.out.print(s + " "));

        System.out.print("\nIntStream = ");

        /*
         * mapToInt(): It returns IntStream after applying the given
         * function.
         */
        IntStream intStream = list.stream().mapToInt(i -> i * i);
        intStream.forEach(s -> System.out.print(s + " "));
    }
}
Output
Stream = 1 4 9 16 
DoubleStream = 1.0 4.0 9.0 16.0 
LongStream = 1 4 9 16 
IntStream = 1 4 9 16 
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_map_methods_App.zip?attredirects=0&d=1

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

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