Tuesday 29 August 2017

How to find the max and min element using Java 8 Stream | Streams in Java 8


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

StreamMaxMinDemo
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class StreamMaxMinDemo
{
    public static void main(String[] args)
    {
        List<String> list = Arrays.asList("Z", "B", "I", "E");

        /*
         * max(): It finds maximum element for the given Comparator.
         */
        String max = list.stream()
                         .max(Comparator.comparing(String::valueOf)).get();
        
        System.out.println("Max:" + max);

        /*
         * min(): It finds minimum element for the given Comparator.
         */
        String min = list.stream()
                         .min(Comparator.comparing(String::valueOf)).get();
        
        System.out.println("Min:" + min);
    }
}
Output
Max:Z
Min:B

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

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

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