1.8 新特性之 Lambda Expressions
Lambda expressions are allowed only at source level 1.8 or above
The target type of this expression must be a functional interface
https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
由来:
One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear. In these cases, you're usually trying to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button. Lambda expressions enable you to do this, to treat functionality as method argument, or code as data.
The previous section, Anonymous Classes, shows you how to implement a base class without giving it a name. Although this is often more concise than a named class, for classes with only one method, even an anonymous class seems a bit excessive and cumbersome. Lambda expressions let you express instances of single-method classes more compactly.
应用对象(functional interface)
A functional interface is any interface that contains only one abstract method.
语法:
- A comma-separated list of formal parameters enclosed in parentheses.
- The arrow token,
->
- A body, which consists of a single expression or a statement block.
举例(参数类型可以省略,单参数可省略() parentheses,单语句可省{}brace):
import org.junit.Test; public class SomeoneTest { @Test
public void test1() {
Someone a = new Someone() {
@Override
public String doSomething(String arg) {
return "::" + arg;
}
}; System.out.println(a.doSomething("a"));
} @Test
public void test2() {
Someone b = (String arg) -> {
return "::" + arg;
};
System.out.println(b.doSomething("b"));
} @Test
public void test3() {
Someone c = arg -> "::" + arg;
System.out.println(c.doSomething("c"));
} } interface Someone {
String doSomething(String arg);
}
多参数必须加()
import org.junit.Test; public class DogTest { @Test
public void test1() {
Dog a = (s1, s2) -> {
System.out.println(s1 + s2);
};
a.bark("wang", "wang");
} @Test
public void test2() {
Dog b = (s1, s2) -> System.out.println(s1 + s2);
b.bark("wang", "wang");
} } interface Dog {
void bark(String s1, String s2);
}
无参数必须是 () ->
import org.junit.Test; public class CatTest { @Test
public void test1() {
Cat a = () -> System.out.println("miao~");
a.meow();
} } interface Cat {
void meow();
}
更多高级用法见 package java.util.function;
1.8 新特性之 Lambda Expressions的更多相关文章
- jdk8新特性之lambda expressions
本文分两部分: 语法简单说明 lambda的使用 注:这两部分内容均以类+注释的方式进行说明,并且内容均来自官方教程(https://docs.oracle.com/javase/tutorial/j ...
- Java8新特性系列-Lambda
转载自:Java8新特性系列-Lambda – 微爱博客 Lambda Expressions in Java 8 Lambda 表达式是 Java 8 最流行的特性.它们将函数式编程概念引入 Jav ...
- java8新特性: lambda表达式:直接获得某个list/array/对象里面的字段集合
java8新特性: lambda表达式:直接获得某个list/array/对象里面的字段集合 比如,我有一张表: entity Category.java service CategoryServic ...
- JDK8新特性之一Lambda
JDK8的新特性之一Lambda能将函数作为方法里面的参数使用. /** * JDK8新特性Lambda */ public class Test { public static void main( ...
- Java8 新特性学习 Lambda表达式 和 Stream 用法案例
Java8 新特性学习 Lambda表达式 和 Stream 用法案例 学习参考文章: https://www.cnblogs.com/coprince/p/8692972.html 1.使用lamb ...
- jdk1.8新特性之lambda表达式及在Android Studio中的使用举例
Jdk1.8已经出很久了但是很多同学对它的特性在android studio 中的应用可能还不是很熟悉,今天我们就来对这个新特性在AS中做它的应用实践. 一.首先在有JDK1.8的情况下我们要在AS的 ...
- Java 8新特性之lambda(八恶人-2)
Major Marquis Warren 沃伦·马奎斯少校 “Tring to get a couple of bounties in to Red Rock.”我想带几个通缉犯去红石镇 一.基本介绍 ...
- Java8新特性之Lambda表达式
lambda表达式是java8给我们带来的几个重量级新特性之一,借用lambda表达式,可以让我们的java程序设计更加简洁.最近新的项目摒弃了1.6的版本,全面基于java8进行开发,本文是java ...
- C++11新特性之一——Lambda表达式
C++11新特性总结可以参考:http://www.cnblogs.com/pzhfei/archive/2013/03/02/CPP_new_feature.html#section_6.8 C++ ...
随机推荐
- Java八大排序算法
Java八大排序算法: package sort; import java.util.ArrayList; import java.util.Arrays; import java.util.List ...
- MySQL——navicat 连接 mysql 出现1251Client does not support authentication protocol requested by server的解决方案
前期修改root密码问题(首次安装的root密码是空,直接Enter就行): cmd用管理员身份进入,然后输入 mysqladmin -u root -p password newpassword 需 ...
- python使用 openpyxl包 excel读取与写入
'''### 写入操作 ###from openpyxl import Workbook#实例化对象wb=Workbook()#创建表ws1=wb.create_sheet('work',0) #默认 ...
- 关于PHP代码复用‘traits’的一段代码
附:代码摘自菜鸟教程 <?php// 定义一个类名Base对象,并带有公共函数sayHello class Base { public function sayHello() { echo 'H ...
- python3之time、datetime、random
UTC:协调世界时,又称世界统一时间.世界标准时间.国际协调时间.由于英文(CUT)和法文(TUC)的缩写不同,作为妥协,简称UTC. 中国属于东八区,领先世界时间8小时 time模块 time.ti ...
- Python入门教程丨1300多行代码,让你轻松掌握基础知识点
欢迎关注小编,除了分享技术文章之外还有很多福利,私信学习资料可以领取包括不限于Python实战演练.PDF电子文档.面试集锦.学习资料等. 前言 本文适合有经验的程序员尽快进入Python世界.特别地 ...
- How to solve “Dynamic Web Module 3.1 requires Java 1.7 or newer” in Eclipse
How to solve “Dynamic Web Module 3.1 requires Java 1.7 or newer” in Eclipse Last updated on June 20t ...
- HDFS 常用命令行:
1. 查看各库的存储大小 hdfs dfs -du -h /user/hive/warehouse 2. 删除HDFS 文件 hdfs dfs -rmr 绝对路径名 例如:hdfs dfs -rmr ...
- python常用库,包网址
常用包下载:https://pypi.org/ 1.NumPy: https://www.numpy.org/ 2.pandas: http://pandas.pydata.org/ 3.SciPy: ...
- python 的类装饰器
我们都知道python的函数有装饰器,那么类也有装饰器吗?有的,为什么没有呢,来看下代码吧 def out(args): def inner(cls): cls._args = args return ...