Java8中的Lambda表达式 - Go语言中文社区

Java8中的Lambda表达式


一、Lambda 表达式的基础语法:

  • Java8中引入了一个新的操作符“->”该操作符称为箭头操作符或Lambda操作符
    它将表达式拆成两部分
    左侧:Lambda 表达式的参数列表
    右侧:lambda 表达式中所需执行的功j能,即Lambda体
  • 语法格式一:无参,无返回值
    ()->System.out.println(“HelloWorld”);
  • 语法格式二:有一个参数,并且无返回值
    (x)->System.out.println(“HelloWorld”+x);
    x->System.out.println(“HelloWorld”+x);
  • 语法格式三:有多个参数,有返回值,并且Lambda体中有多条语句
    Comparator com = (x,y)->{System.out.println(“x=”+x+",y="+y);return x+y;};
    System.out.println(com.compare(1,3));
  • 语法格式四:Lambda体中只有一条语句则,return和大括号可以不写
    (x,y)->x+y;
  • 语法格式五:Lambda表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下玩推断出,数据类型,即“类型推断”
    (Integer x,Integer y)->x+y;

二、Lambda表达式需要“函数式接口”的支持

函数式接口:接口中只有一个抽象方法的接口称为函数式接口,可以用注解@FunctionalInterface 修饰,该注解用来检查是否是函数式接口(用了该注解就只能有一个抽象方法,凡是不满足条件的就会报错)

Created by CHEN on 2019/8/2.

三、常见用法

  • Runnable接口匿名类:
//1.7中的实现
    Runnable r = new Runnable() {
            @Override
            public void run() {
                System.out.println("helloWodld");
            }
        };
        r.run();
        System.out.println("--------------------------------");
        //1.8中Lambda表达式
        Runnable r2 = () -> System.out.println("hello Lambda!");
        r2.run();
  • 重写Compare方法
     Comparator<Integer> com = (x, y) -> {
            System.out.println("x=" + x + ",y=" + y);
            return x + y;
        };
        System.out.println(com.compare(1, 3));
  • 输出结果:

image.png

  • 自定义函数式接口:
//自定义函数式接口
@FunctionalInterface
public interface MyFunction2<R,T> {
    public R getResult(T t1,T t2);
}

    //泛型处理方法
    public void getResult(Long l1,Long l2 ,MyFunction2<Long,Long> myFunction2){
        System.out.println(myFunction2.getResult(l1,l2));
    }
    //测试
    @Test
    public void test4(){
        getResult(100L,200L,(x,y)->x+y);
    }

  • 结果:

image.png

Java8中内置的四大函数式接口

  Consumer<T>: 消费型接口
       void accept(T t);
 
  Supplier<T>:供给型接口
       T get();
 
  Function<T ,R>:函数型接口
       R apply(T t);
 
  Predicate<T> :断言型接口
       boolean test(T t);
  • 这四种接口可以涵盖我们日常需要,只需针对函数式接口写调用方法,用Lambda表达式传入实现方法,就可以快速实现不同种函数,十分类似于C#种的委托(我先了解的C#种的委托机制和Lambda表达式等,两者真的十分相像包括StreamAPI也是…)

方法引用、构造器引用与数组引用

一、方法引用:若 Lambda 体中的功能,已经有方法提供了实现,可以使用方法引用(可以将方法引用理解为 Lambda 表达式的另外一种表现形式)

 1. 对象的引用 :: 实例方法名
 
  2. 类名 :: 静态方法名
  
  3. 类名 :: 实例方法名
//对象的引用 :: 实例方法名
	@Test
	public void test2(){
		Employee emp = new Employee(101, "张三", 18, 9999.99);
		
		Supplier<String> sup = () -> emp.getName();
		System.out.println(sup.get());
		
		System.out.println("----------------------------------");
		
		Supplier<String> sup2 = emp::getName;
		System.out.println(sup2.get());
	}
  • 结果

image.png

//类名 :: 静态方法名
	@Test
	public void test3(){
		BiFunction<Double, Double, Double> fun = (x, y) -> Math.max(x, y);
		System.out.println(fun.apply(1.5, 22.2));
		
		System.out.println("--------------------------------------------------");
		
		BiFunction<Double, Double, Double> fun2 = Math::max;
		System.out.println(fun2.apply(1.2, 1.5));
	}
  • 结果:

image.png


	//类名 :: 实例方法名
	@Test
	public void test5(){
		BiPredicate<String, String> bp = (x, y) -> x.equals(y);
		System.out.println(bp.test("abcde", "abcde"));
		
		System.out.println("-----------------------------------------");
		
		BiPredicate<String, String> bp2 = String::equals;
		System.out.println(bp2.test("abc", "abc"));
		
		System.out.println("-----------------------------------------");
		
		
		Function<Employee, String> fun = (e) -> e.show();
		System.out.println(fun.apply(new Employee()));
		
		System.out.println("-----------------------------------------");
		
		Function<Employee, String> fun2 = Employee::show;
		System.out.println(fun2.apply(new Employee()));
		
	}
  • 结果:

image.png

注意:

①方法引用所引用的方法的参数列表与返回值类型,需要与函数式接口中抽象方法的参数列表和返回值类型保持一致!
②若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式: ClassName::MethodName

二、构造器引用 :构造器的参数列表,需要与函数式接口中参数列表保持一致!

  
 1. 类名 :: new
@Test
	public void test7(){
		Function<String, Employee> fun = Employee::new;
		
		BiFunction<String, Integer, Employee> fun2 = Employee::new;
	}

三、数组引用

类型[] :: new;
  
//数组引用
	@Test
	public void test8(){
		Function<Integer, String[]> fun = (args) -> new String[args];
		String[] strs = fun.apply(10);
		System.out.println(strs.length);

		System.out.println("--------------------------");

		Function<Integer, Employee[]> fun2 = Employee[] :: new;
		Employee[] emps = fun2.apply(20);
		System.out.println(emps.length);
	}
  • 结果:
    image.png
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/symuamua/article/details/98730304
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢