Java 8 foreach函数中有超过1个命令 [英] More then 1 command in the Java 8 foreach function
本文介绍了Java 8 foreach函数中有超过1个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在使用Java 8引入的Map.foreach函数中使用多个命令?
Is it possible to use more than one command in the Map.foreach function that got introduced with Java 8?
所以相反:
map.forEach((k, v) ->
System.out.println(k + "=" + v));
我想做类似的事情:
map.forEach((k, v) ->
System.out.println(k)), v.forEach(t->System.out.print(t.getDescription()));
假设k是字符串而v是集合。
Lets pretend that k are Strings and v are Sets.
推荐答案
lambda语法允许对正文进行两种定义:
The lambda syntax allows two kinds of definitions for the body:
- 单个值返回表达式,例如:
x - > x * 2
- 多个语句,用大括号括起来,例如:
x - > {x * = 2;返回x; }
- a single, value-returning, expression, eg:
x -> x*2
- multiple statements, enclosed in curly braces, eg:
x -> { x *= 2; return x; }
第三种特殊情况是允许您在调用时避免使用花括号的情况 void
返回方法,例如: x - > System.out.println(x)
。
A third special case is the one that allows you to avoid using curly braces, when invoking a void
returning method, eg: x -> System.out.println(x)
.
这篇关于Java 8 foreach函数中有超过1个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文