Thursday 24 August 2017

Terminal operations[match] in Java 8 Stream | Streams in Java 8


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

Click the below Image to Enlarge
Terminal operations[match] in Java 8 Stream | Streams in Java 8 
StreamMatchDemo.java
import java.util.Arrays;
import java.util.List;

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

        /*
         * Returns:true if either all elements of the stream match the
         * provided predicate or the stream is empty, otherwise false
         */
        boolean isAllNumbersLargerThanFive = numberList.stream()
                                                        .allMatch(i -> i > 5);
        System.out.println(isAllNumbersLargerThanFive); // false

        /*
         * Returns:true if any elements of the stream match the
         * provided predicate, otherwise false
         */
        boolean hasNumberLargerThanFive = numberList.stream()
                                                    .anyMatch(i -> i > 5);
        System.out.println(hasNumberLargerThanFive); // true

        /*
         * Returns:true if either no elements of the stream match the
         * provided predicate or the stream is empty, otherwise false
         */
        boolean isNoneNumberLargerThanTen = numberList.stream()
                                                      .noneMatch(i -> i > 10);
        System.out.println(isNoneNumberLargerThanTen); // true
    }
}
Output
false
true
true
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_Ter_Op_match_App.zip?attredirects=0&d=1

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

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