Thursday, 15 June 2017

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


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

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

class Numbers
{
    public static boolean isLessThanFifty(int number)
    {
        return number < 50;
    }

    public static List<Integer> findNumbers(List<Integer> list,
                                                Predicate<Integer> p)
    {
        List<Integer> newList = new ArrayList<>();
        for (Integer i : list)
        {
            /*
             * test the number is less than 50
             * then add to the newList.
             */
            if (p.test(i))
            {
                newList.add(i);
            }
        }
        return newList;
    }
}
LambdaDemo.java
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

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

        List<Integer> list = Arrays.asList(12, 55, 75, 88, 24, 40);

        // Using an anonymous class
        List<Integer> listOfLessthanfiftyNumbers = Numbers.findNumbers(list, new Predicate<Integer>()
        {
            public boolean test(Integer i)
            {
                return Numbers.isLessThanFifty(i);
            }
        });
        
        System.out.println("listOfLessthanfiftyNumbers - anonymous class = "+listOfLessthanfiftyNumbers);
        
        // Using a lambda expression
        listOfLessthanfiftyNumbers = Numbers.findNumbers(list, (i) -> Numbers.isLessThanFifty(i));
        System.out.println("listOfLessthanfiftyNumbers - lambda expression = "+listOfLessthanfiftyNumbers);
        
        // Using a method reference
        listOfLessthanfiftyNumbers = Numbers.findNumbers(list, Numbers::isLessThanFifty);
        System.out.println("listOfLessthanfiftyNumbers - method reference = "+listOfLessthanfiftyNumbers);
    }

}
Output
listOfLessthanfiftyNumbers - anonymous class = [12, 24, 40]
listOfLessthanfiftyNumbers - lambda expression = [12, 24, 40]
listOfLessthanfiftyNumbers - method reference = [12, 24, 40]

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

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

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

    Google+ Followers

    Google+ Badge