Java中Lambda表达式用法介绍

Lambda

lambda是一个匿名函数,我们可以把lambda表达式理解为是一段可以传递的代码。

lambda简明的地将代码或方法作为参数传递进去执行。 “函数式编程”其核心是把函数作为值。 函数式接口 :只有一个 抽象方法的接口 称之为 函数式接口。函数式接口可以使用@FunctionalInterface进行注解。

lambda表达式拆分为两部分

左侧:lambda 表达式的参数列表

右侧:lambda 表达式中所需要执行的功能,即lambda体

语法格式一:无参数,无返回值

@Testpublic void test(){    // () -> System.out.println("Hello");    Runnable a = new Runnable(){     @Override     public void run(){        System.out.println("Hello")    }    };    //等同于    Runnable a1 = () -> System.out.println("Hello");    a1.run();}

语法格式二:有一个参数,无返回值(若只有一个参数 小括号可以省略不写)

@Testpublic void test(){    //Consumer被注解@FunctionalInterface的接口(函数式接口) 唯一抽象方法 void accept(T t);    //左侧参数 -> 右侧执行体    Consumer<String> con = (x) -> System.out.println(x);                         // x -> System.out.println(x);    con.accept("hahah");}

语法格式三:有两个以上的参数,并且lambda体中有多条语句 (若lambda体中只有一条语句,return 和 大括号都可以省略不写)

@Testpublic void test(){    //Comparator被注解@FunctionalInterface的接口 举例抽象方法 int compare(T o1,T o2);    Comparator<Integer> com = (x,y) -> {      System.out.println("hhaha0");      return (x < y) ? -1 : ((x == y) ? 0 : 1);    };    com.compare(1,2);}

注意:lambda表达式的参数类型可以省略不写,因为jvm编译器可以从上下文推断出数据类型。即“类型推断”如果要在参数里面写数据类型,都要写上。

实例实例1:

class Employee {    private String name;    private int age;    private double salary;?    //省略 get and set and constructor?}interface MyPredicate<T> {    boolean test(T t);}public class Test{    static List<Employee> list = Arrays.asList(            new Employee("张三",10,1),            new Employee("里斯",20,1),            new Employee("王五",16,1),            new Employee("二三",30,1)    );    public static List<Employee> filterEmployee(List<Employee> list,MyPredicate<Employee> mp){        List<Employee> emps = new ArrayList<>();        for (Employee employee : list) {            if(mp.test(employee)){                emps.add(employee);            }        }        return emps; }    @org.junit.Test    public void test1(){        //需要使用自定义的方法        List<Employee> list2 = filterEmployee(list,(e) -> e.getAge() >= 15);        list2.stream().map(Employee::getName).forEach(System.out::println);    }    @org.junit.Test    public void test2(){        //可以使用stream进行list集合的过滤  不使用自定义接口        List<Employee> list2 = list.stream().filter((e) -> e.getAge() >= 15).collect(Collectors.toList());        list2.stream().map(Employee::getName).forEach(System.out::println);    }}

实例2:

创建一个MyFun接口使用@FunctionalInterface注解,并创建一个抽象方法Integer getValue(Integer num);在Test类对变量进行某种操作。

@FunctionalInterfaceinterface MyFun{    Integer getValue(Integer num);}public class Test{    @org.junit.Test    public void Test(){        operation(100,num -> ++num);    }    /**    * param1 num : 传入的整形数    * param2 mf : 实现某种方式对 整形数 进行操作。    **/    public Integer operation(Integer num,MyFun mf){        return mf.getValue(num);    }}
class Employee {    private String name;    private int age;    private double salary;?    @Override    public String toString() {        return "["+this.name+","+this.getAge()+","+this.getSalary()+"]";    }    //省略 getter and setter  and constructor}?public class Test {    List<Employee> list = Arrays.asList(            new com.bilibili.lambda.test1.Employee("张三",10,1),            new com.bilibili.lambda.test1.Employee("里斯",20,1),            new com.bilibili.lambda.test1.Employee("王五",16,1),            new Employee("二三",30,1)    );    @org.junit.Test    public void test(){        Collections.sort(list,(e1,e2) -> {            if(e1.getAge() == e2.getAge()){                return e1.getName().compareTo(e2.getName());            }else{                //比较年龄大小                return Integer.compare(e1.getAge(),e2.getAge());            }        });        for (Employee e: list) {            System.out.println(e);        }    }}

四大核心函数式接口 Consumer<T> : 消费性接口 void accept(T t); Supplier<T> : 共给性接口 T get(); Function<T,R> : 函数性接口 T代表参数,R代表返回值 R apply(T t); Predicate<T> :断言性接口 boolean test(T t);

 class Test{    @org.junit.Test    publilc void test(){        happy(10000,(money)->System.out.println("happy消费"+money+"元"));    }    public void happy(double money,Consumer<double> con){        con.accept(money);    }}

lambda方法引用

方法引用:若lambda体中的内同有方法已经实现了,我们可以使用“方法引用”

(可以理解为方法引用时lambda的另一种表现形式)

主要有三种语法格式:

对象::实例方法名 类::静态方法名 类::实例方法名

class Test{    //对象::实例方法名    @org.junit.Test    public void test(){        Consumer<String> con = (x) -> System.out.println(x);        con.accept("haha");        Consumer<String> con2 = System.out::println;        con2.accept("haha");    }    //类::静态方法名    @org.junit.Test    public void test2(){        Comparator<Integer> com = (x,y) -> Integer.compare(x,y);        Comparator<Integer> com2 = Integer::compare;        com.compare(1,2);        com2.compare(1,2);    }    //类::实例方法名    @org.junit.Test(){        BiPredicate<String,String> bp = (x,y) -> x.equals(y);        bp.test("a","a");        BiPredicate<String,String> bp2 = String::equals;    }}

lambda构造器引用

格式:

CalssName::new

class Test{    @org.junit.Test    public void test(){        Supplier<String> sup = () -> new String();        //这里的构造器引用取决于 接口方法的参数 的个数。 此处函数式接口 T get(); 为无参抽象方法所以String在实例化时 也是实例化无参的构造方法  其他类也适用        Supplier<String> sup2 = String::new;        String str = sup2.get();    }}

到此这篇关于Java中Lambda表达式用法介绍的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。

何愁没有快乐的泉溪在歌唱,何愁没有快乐的鲜花绽放!

Java中Lambda表达式用法介绍

相关文章:

你感兴趣的文章:

标签云: