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
  • Google+ Followers

    Google+ Badge