Thursday 24 August 2017

Terminal operations[min and max] in Java 8 Stream | Streams in Java 8


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

Click the below Image to Enlarge
Terminal operations[min and max] in Java 8 Stream | Streams in Java 8 
StreamMaxDemo.java
import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Stream;

public class StreamMaxDemo
{
    public static void main(String[] args)
    {

        Optional<String> optional = Stream.of("A", "B", "C")
                .max(new Comparator<String>()
                {
                    @Override
                    public int compare(String o1, String o2)
                    {
                        return o1.compareTo(o2);
                    }
                });
        System.out.println("optional = "+optional);
        System.out.println("Max = "+optional.get());
    }
}
Output
optional = Optional[C]
Max = C


StreamMinMaxDemo.java
import java.util.OptionalInt;
import java.util.stream.IntStream;

public class StreamMinMaxDemo
{
    public static void main(String[] args)
    {
        OptionalInt optionalIntMin = IntStream.of(1, 2, 3).min();
        System.out.println("optionalIntMin = " + optionalIntMin);
        System.out.println("min = " + optionalIntMin.getAsInt());

        OptionalInt optionalIntMax = IntStream.of(1, 2, 3).max();
        System.out.println("optionalIntMax = " + optionalIntMax);
        System.out.println("max = " + optionalIntMax.getAsInt());

    }
}
Output
optionalIntMin = OptionalInt[1]
min = 1
optionalIntMax = OptionalInt[3]
max = 3

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

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

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