好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

浅谈Java中Lambda表达式的相关操作

为什么要使用lambda?

可以对一个接口进行非常简洁的实现。

lambda对接口的要求?

接口中定义的抽象方法有且只有一个才可以。

传统实现一个接口需要这样做:

方法一:

?

1

2

3

4

5

6

7

8

9

10

11

// 实现接口,同时必须重写接口中抽象方法

class test implements intrfacefn {

     @override

     public void getuser( int a, int b) {

     }

}

// @functionalinterface 注解意思:函数式接口,用来做规范,有这个注解,说明此接口有且只有一个抽象方法!!!

@functionalinterface

interface intrfacefn{

     public void getuser( int a, int b);

}

方法二:
匿名表达式

?

1

2

3

4

5

6

7

8

9

10

11

public class lamda {

     public static void main(string[] args) {

         // 匿名表达式实现接口

         intrfacefn intrfacefn1 = new intrfacefn(){

             @override

             public void getuser( int a, int b) {

                

             }

         };

     }

}

使用lambda -> 只关注参数和方法体 (返回值类型不需要写、类型不需要写)

?

1

2

3

4

5

6

7

public class lamda {

     public static void main(string[] args) {

         // 实现接口,后边匿名函数就是重写的方法!

         intrfacefn intrfacefn = ( int a, int b) -> system.out.println(a-b);

         intrfacefn.getuser( 1 , 2 );

     }

}

不定参

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@functionalinterface

interface intrfacefn{

     public void getuser( int ... a);

}

public class lamda {

     public static void main(string[] args) {

         intrfacefn intrfacefn = ( int ...a) -> {

             for ( int i = 0 ; i < a.length; i ++) {

                 system.out.println(a[i]);

             }

         };

         intrfacefn.getuser( 1 , 2 );

     }

}

可省略的部分

参数类型

?

1

intrfacefn intrfacefn = (a, b) -> system.out.println(a-b);

小括号
前提只有一个参数情况

?

1

intrfacefn intrfacefn = a -> system.out.println(a);

方法大括号

方法体只有一句代码

?

1

intrfacefn intrfacefn = (a, b) -> system.out.println(a-b);

返回return

如果大括号中只有一条返回语句,则return 也可以省略

?

1

2

3

4

5

intrfacefn intrfacefn = (a, b) -> {

     return a-b

};

// 省略之后写法:

intrfacefn intrfacefn = (a, b) -> a-b;

高级部分

方法的引用

将一个lambda表达式的实现指向一个已实现的方法,这样做相当于公共逻辑部分的抽离,实现复用。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public class lamda {

     public static void main(string[] args) {

         intrfacefn intrfacefn = (a, b) -> add(a, b);

         intrfacefn.getuser( 1 , 2 );

     }

     public static void add( int a, int b) {

         system.out.println(a+b);

     }

}

 

@functionalinterface

interface intrfacefn{

     public void getuser( int a, int b);

}

还有更简洁的实现:
方法隶属者: 语法 - 方法隶属者 :: 方法名
补充下:这个方法隶属者,主要看方法是类方法还是对象方法,如果是类 -  方法类::方法名  ,如果是对象方法 - new 方法类::方法名

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public class lamda {

     public static void main(string[] args) {

         intrfacefn intrfacefn = lamda::add;

         intrfacefn.getuser( 1 , 2 );

     }

     public static void add( int a, int b) {

         system.out.println(a+b);

     }

}

 

@functionalinterface

interface intrfacefn{

     public void getuser( int a, int b);

}

到此这篇关于浅谈java中lambda表达式的相关操作的文章就介绍到这了,更多相关java lambda表达式内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_42778001/article/details/118354490

查看更多关于浅谈Java中Lambda表达式的相关操作的详细内容...

  阅读:16次