How to search MAX and MIN of a array in Java 8 .

 




  

 This  code  explains how to find  out max  and min value of  an array  using java  8. Here in 


 sceond class there is option to choose  range  to find  max and min value . In first class array

 converted in stream and then collected the max value and another class Instream is used .



import java.util.Arrays;

 

class  YellowMaxMin

{

    public static void main(String[] args)

    {

        int[] A = { 6, 8, 3, 5, 1, 9, 4, 2 };

 

        int max = Arrays.stream(A)

                        .max()

                        .getAsInt();

 

        int min = Arrays.stream(A)

                        .min()

                        .getAsInt();

 

        System.out.println("Min element is: " + min);

        System.out.println("Max element is :" + max);

    }

}



 Outout  of the code is :


   Min element is: 1

   Max element is: 9



import java.util.stream.IntStream;

 

class YellowMaxMin

{

    public static void main(String[] args)

    {

        int[] A = { 6, 8, 3, 5, 1, 9, 4 ,2 };

 

        int max = IntStream.range(0, A.length)

                        .map(i -> A[i])

                        .max()

                        .getAsInt();

 

        int min = IntStream.range(0, A.length)

                        .map(i -> A[i])

                        .min()

                        .getAsInt();

 

        System.out.println("Min element is " + min);

        System.out.println("Max element is " + max);

    }

}


 Code output :


Min element is: 1

 Max element is: 9



How to search MAX and MIN of a array in Java 8 . How  to search  MAX and MIN   of a array  in Java 8 . Reviewed by Mukesh Jha on 4:11 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.