Java for 语句简化写法_Java 8 Lambda 写法与简化

Java 8 的 Lambda 表达式的实现方式还是基于已有的字节码指令,由 Lambda 表达式的方法签名结合上下文并经由 SAM 推断出正确的类型来。Java 8 的 Lambda 完整书写格式是

(type parameter1 [type parameter2, ...type parametern]) -> { statements here }

这种标准格式所表示的就是方法签名。

虽不及其他语言的 Lambda 表达式,像 Swift, Scala 可省略参数部分,可用默认参数名 $0, $1, 或 _, 但 Java 8 的 Lambda 还是可以进行酌情简化

参数类型总是可省略   --     (x, y) -> { x + y; }

参数为一个时,参数括号可省略  --    x -> { System.out.println(x); }

语句为一条时,可省略大括号, 并且语句后不要分号 --  x -> System.out.println(x)

上面更进一步,如果是单条 return 语句,必须把 return 关键字去掉  --  x -> "Hello " + x

就差一点,参数部分总是不能省,无参必须写成 () -> System.out.println("hi")

Java 8 中若要近似的实现无参数部分写法,那就是方法引用了 -- System.out::println

. 参数类型总是可省略, 基本上我们总是省略掉参数类型

interface Function {

void call(String x, String y);

}

void foo(Function f) {

f.call("a", "b");

}

foo((x, y) -> {     //(x, y) 完整写法是 (String x, String y)

System.out.println(x);

System.out.println(y);

});

1

2

3

4

5

6

7

8

9

10

11

12

interfaceFunction{

voidcall(Stringx,Stringy);

}

voidfoo(Functionf){

f.call("a","b");

}

foo((x,y)->{    //(x, y) 完整写法是 (String x, String y)

System.out.println(x);

System.out.println(y);

});

. 参数为一个时,参数括号省略

interface Function {

void call(String x);

}

Function f = x -> {

System.out.println();

System.out.println(x);

};

f.call("Hello");

1

2

3

4

5

6

7

8

9

interfaceFunction{

voidcall(Stringx);

}

Functionf=x->{

System.out.println();

System.out.println(x);

};

f.call("Hello");

. 语句为一条时,可省略大括号, 并且语句后不要分号

interface Function {

void call(String x);

}

void foo(Function f) {

f.call("Hi");

}

foo(x -> System.out.println(x)); //不能写成 foo(x -> System.out.println(x);); 这像话

1

2

3

4

5

6

7

8

9

interfaceFunction{

voidcall(Stringx);

}

voidfoo(Functionf){

f.call("Hi");

}

foo(x->System.out.println(x));//不能写成 foo(x -> System.out.println(x);); 这像话

. 如果是单条 return 语句,省去大括时必须把 return 关键字去掉

interface Function {

void String call(String x);

}

foo(x -> "Hello " + x)

1

2

3

4

5

interfaceFunction{

voidStringcall(Stringx);

}

foo(x->"Hello "+x)

. 参数部分的括号总是需要

interface Function {

void call();

}

Function f = () -> System.out.println();

1

2

3

4

5

interfaceFunction{

voidcall();

}

Functionf=()->System.out.println();

不写出 -> 来似乎不知道这是一个 Lambda.

Java  8 中目前还无法简化成类似于 Swift/Scala 中的 foo(System.out.println()) 或 foo {System.out.println()}。不过可以采用方法引用的方式,只是方法引用有其缺陷--不能捕获外部变量。

public class TestLambda {

public static void main(String[] args) {

foo(TestLambda::todo); //todo 方法的签名正好是 x -> String

}

static void foo(Function f){

System.out.println(f.call("World"));

}

static String todo(String x) { //也就是这个方法正好符合 Function 的 SAM 方法签名

return "Hello " + x;

}

}

interface Function {

String call(String x);

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

publicclassTestLambda{

publicstaticvoidmain(String[]args){

foo(TestLambda::todo);//todo 方法的签名正好是 x -> String

}

staticvoidfoo(Functionf){

System.out.println(f.call("World"));

}

staticStringtodo(Stringx){//也就是这个方法正好符合 Function 的 SAM 方法签名

return"Hello "+x;

}

}

interfaceFunction{

Stringcall(Stringx);

}

前面说过用方法引用的缺陷是无法捕外部变量,譬如用 Lambda 的方式

String external = "ABC";

foo(x -> external + x);

1

2

Stringexternal="ABC";

foo(x->external+x);

而在前面的 todo 方法中却访问不到 external 变量,如果给 todo 再增加一个参数来传递 external 则不符合 Function 的 SAM 的签名了。

除非,除非用实例的方法引用,那么需要每次传入外部参数来构造实例,例如这样

public class TestLambda {

public static void main(String[] args) {

String external = "ABC";

foo(new MethodProvider(external)::todo); //输出 Hello ABC World

}

static void foo(Function f){

System.out.println(f.call("World"));

}

}

class MethodProvider {

private String _external;

public MethodProvider(String external) {

this._external = external;

}

String todo(String x) {

return "Hello " + _external + " " + x;

}

}

interface Function {

String call(String x);

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

publicclassTestLambda{

publicstaticvoidmain(String[]args){

Stringexternal="ABC";

foo(newMethodProvider(external)::todo);//输出 Hello ABC World

}

staticvoidfoo(Functionf){

System.out.println(f.call("World"));

}

}

classMethodProvider{

privateString_external;

publicMethodProvider(Stringexternal){

this._external=external;

}

Stringtodo(Stringx){

return"Hello "+_external+" "+x;

}

}

interfaceFunction{

Stringcall(Stringx);

}

这样着实有些怪异,而且有点把欲作简化的东西复杂化了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值