(这里写参数) -> {这里写函数体}
当然如果参数和函数体很少,就可以不写外面的区域符号,例如persion -> "100"
这个看不懂没关系,我只是说明下有这种情况
@FunctionalInterface
interface WrapUser{
void insert(User user);
}
User user = new User("hongbiao", 25);
WrapUser wu = User -> System.out.print(user.getName()+"--"+user.getAge());
wu.insert(user);
解释下,这段代码,第一行是创建一个对象,第二行是实例化这个接口,实例化这个接口就要实现这个接口里面的所有抽象的方法,返回的是一个接口对象
wu
, 最后一步就是调用这个接口里面的方法。
主要是第二句 User表示的是需要的参数类型,-> 后面的是函数体,因为这里只有一句话所以省略了花括号,也可以这样写
WrapUser wu = (User) -> {System.out.print(user.getName()+"--"+user.getAge());};
(这里写参数) -> {function()}
接口,无返回值
@FunctionalInterface
interface WrapUser{
void insert(User user);
}
有返回值的方法和无返回值的方法
static void delet(){
System.out.print("delete---");
}
static int add(){
System.out.print("delete---");
return 1;
}
使用,这样一来,lamdba表达式调用函数,无论函数是否有返回值都不报错
User user = new User("hongbiao", 25);
WrapUser wu = User -> System.out.print(user.getName()+"--"+user.getAge());
wu.insert(user);
WrapUser wu2 = User -> delet();
wu.insert(user);
WrapUser wu3 = User -> add();
wu.insert(user);
接口,有返回值 此时的接口是一个有返回值的接口
@FunctionalInterface
interface MapSet{
int get();
}
有返回值和无返回值的方法
static int getAge(){
return 25;
}
static void delet(){
System.out.print("delete---");
}
使用
这里写代码片
MapSet map = () -> 100;
map.get();
MapSet map2 = () -> getAge();
map.get();
/**
* 接口中需要返回值,而delete这个函数没有返回值,因此报错
*/
MapSet map3 = () -> delet(); //这里是报错的
map.get();
lamdba
,是不是所有的接口都可以这样写呢,当然不是,所谓的函数式编程是指一个有且只有一个抽象方法的接口,当然 用default和static修饰的方法除外,这两个关键字是java8 里面的新特性,例如我们看一下系统接口Runable这个接口,在接口上一行使用@FunctionalInterface
注解表明这是一个函数式接口,那么我们就可以使用lamdba表达式来写。package java.lang;
/**
* The Runnable
interface should be implemented by any
* class whose instances are intended to be executed by a thread. The
* class must define a method of no arguments called run
.
*
* This interface is designed to provide a common protocol for objects that
* wish to execute code while they are active. For example,
* Runnable
is implemented by class Thread
.
* Being active simply means that a thread has been started and has not
* yet been stopped.
*
* In addition, Runnable
provides the means for a class to be
* active while not subclassing Thread
. A class that implements
* Runnable
can run without subclassing Thread
* by instantiating a Thread
instance and passing itself in
* as the target. In most cases, the Runnable
interface should
* be used if you are only planning to override the run()
* method and no other Thread
methods.
* This is important because classes should not be subclassed
* unless the programmer intends on modifying or enhancing the fundamental
* behavior of the class.
*
* @author Arthur van Hoff
* @see java.lang.Thread
* @see java.util.concurrent.Callable
* @since JDK1.0
*/
@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface Runnable
is used
* to create a thread, starting the thread causes the object's
* run
method to be called in that separately executing
* thread.
*
* The general contract of the method run
is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
//不使用函数表达式
new Thread(new Runnable() {
@Override
public void run() {
System.out.print("hello biaoge");
}
}).start();
//使用函数表达式
new Thread(()->System.out.print("hello biaoge")).start();
从上面来看,函数表达式明显方便简洁