今天学了下Lambda表达式,入职一年了堕于学习,且一年里一直被一些情绪所影响,如今想来十分不该,男人还是拯救世界用的。
Everything’ll be fine.
So be it.
贴一个很简单的案例吧,就当是扬帆。
package learning;
import java.util.Arrays;
import java.util.List;
/**
* @author SuperBin
*
* @date: 2020年7月20日
*/
public class DoubleColon {
public static void main(String[] args) {
List<String> list = Arrays.asList("a", "b", "c");
// 使用遍历的方法
for (String word : list) {
printValue(word);
}
// 使用Lambda表达式: (变量) -> {代码块},如果代码块只有一个表达式,大括号可以省略
// list.forEach((word) -> {DoubleColon.printValue(word);});
list.forEach(word -> DoubleColon.printValue(word));
// ::是类似于C++的域运算符,获取方法使用的
list.forEach(DoubleColon::printValue);
}
public static void printValue(String word) {
System.out.println(word);
}
}
输出结果
a
b
c
a
b
c
a
b
c