Wednesday, 23 August 2017

Intermediate operations[filter,map,sorted,distinct and limit] of Java 8 Stream | Streams in Java 8


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

Click the below Image to Enlarge
Intermediate operations[filter,map,sorted,distinct and limit] of Java 8 Stream | Streams in Java 8 
Intermediate operations[filter,map,sorted,distinct and limit] of Java 8 Stream | Streams in Java 8 
Intermediate operations[filter,map,sorted,distinct and limit] of Java 8 Stream | Streams in Java 8 
StreamFilterDemo.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamFilterDemo
{
    public static void main(String[] args)
    {
        List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8,9);
        /*
         * The example gets a new stream with even numbers from list
         * of numbers:
         */
        Stream<Integer> stream = numberList.stream()
                                           .filter(i -> (i % 2) == 0);

        stream.forEach(number -> System.out.println(number));

    }
}
Output
2
4
6
8
StreamMapDemo.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamMapDemo
{
    public static void main(String[] args)
    {
        List<String> nameList = Arrays.asList("ram","peter");

        /*
         * The example gets a new stream which all names are
         * transformed from LowerCase to UpperCase.
         */
        Stream<String> stream = nameList.stream()
                                        .map(name -> name.toUpperCase());

        stream.forEach(name -> System.out.println(name));

    }
}
Output
RAM
PETER
StreamDistinctDemo.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamDistinctDemo
{
    public static void main(String[] args)
    {
        List<Integer> numberList = Arrays.asList(1, 1, 2, 3, 3, 4, 5,6, 6);
        /*
         * Returns a stream consisting of the distinct elements
         * (according to Object.equals(Object)) of this stream.
         */
        Stream<Integer> stream = numberList.stream().distinct();
        stream.forEach(System.out::println);
    }
}
Output
1
2
3
4
5
6
StreamSortDemo.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamSortDemo
{
    public static void main(String[] args)
    {
        List<Integer> numberList = Arrays.asList(8, 9, 2, 5, 3, 4, 1, 7, 6);
        /*
         * Returns a stream consisting of the elements of this stream,
         * sorted according to natural order. If the elements of this
         * stream are not Comparable, a java.lang.ClassCastException
         * may be thrown when the terminal operation is executed.
         */
        Stream<Integer> stream = numberList.stream().sorted();
        stream.forEach(System.out::println);
    }
}
Output
1
2
3
4
5
6
7
8
9
StreamLimitDemo.java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamLimitDemo
{
    public static void main(String[] args)
    {
        List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        /*
         * Returns a stream consisting of the elements of this stream,
         * truncated to be no longer than maxSize in length
         */
        Stream<Integer> limstream = numberList.stream().limit(5);
        limstream.forEach(System.out::println);
    }
}
Output
1
2
3
4
5
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_IMO_5_App.zip?attredirects=0&d=1

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

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