Thursday 15 June 2017

Static Method reference - Number more than 50| Method reference in Java 8 | Java method reference


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

Click the below Image to Enlarge
Static Method reference - Number more than 50| Method reference in Java 8 | Java method reference 
Numbers.java
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiPredicate;

class Numbers
{
    public static boolean isMoreThanFifty(int n1, int n2)
    {
        return (n1 + n2) > 50;
    }

    public static List<Integer> findNumbers(List<Integer> list,
                                        BiPredicate<Integer, Integer> p)
    {
        List<Integer> newList = new ArrayList<>();
        for (Integer i : list)
        {
            if (p.test(i, i + 10))
            {
                newList.add(i);
            }
        }
        return newList;
    }
}
LambdaDemo.java
import java.util.Arrays;
import java.util.List;
import java.util.function.BiPredicate;

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

        List<Integer> list = Arrays.asList(12, 5, 45, 18, 33, 24, 40);

        // Using an anonymous class
        List<Integer> listOfNumbers = Numbers.findNumbers(list, new BiPredicate<Integer, Integer>()
        {
            public boolean test(Integer i1, Integer i2)
            {
                return Numbers.isMoreThanFifty(i1, i2);
            }
        });
        
        System.out.println("listOfNumbers using a anonymous class = "+listOfNumbers);
        
        // Using a lambda expression
        listOfNumbers =Numbers.findNumbers(list, (i1, i2) -> Numbers.isMoreThanFifty(i1, i2));
        System.out.println("listOfNumbers using a lambda expression = "+listOfNumbers);
        
        // Using a method reference
        listOfNumbers = Numbers.findNumbers(list, Numbers::isMoreThanFifty);
        System.out.println("listOfNumbers using a method reference = "+listOfNumbers);
    }

}
Output
listOfNumbers using a anonymous class = [45, 33, 24, 40]
listOfNumbers using a lambda expression = [45, 33, 24, 40]
listOfNumbers using a method reference = [45, 33, 24, 40]

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/525432e9afa25ee5052810cb2318f64453ffeffa/BasicJava/LambdaDemo_static_method_ref_more50_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
  • 10 comments:

    1. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.
      Java Training in Chennai

      ReplyDelete
    2. Great Article android based projects

      Java Training in Chennai Project Center in Chennai Java Training in Chennai projects for cse The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      ReplyDelete
    3. I wish to show thanks to you just for bailing me out of this particular
      trouble.As a result of checking through the net and meeting
      techniques that were not productive, I thought my life was done.
      oracle certification in Chennai
      asp net training in Chennai
      C # Training in Chennai

      ReplyDelete
    4. İnstagram takipçi satın al! İnstagram takipçi sitesi ile takipçi satın al sende sosyal medyada fenomen olmaya bir adım at. Sende hemen instagram takipçi satın almak istiyorsan tıkla:

      1- takipçi satın al

      2- takipçi satın al

      3- takipçi satın al

      ReplyDelete