Lamda表达式使用

 public class Lambda {

     public static void main(String[] args) {

         Lambda lambda=new Lambda();
String something="oop print somthing";
String something1="oop print something2";
String something2="lambda print something"; lambda.printSomething(something); Printer printer=new Printer(){
@Override
public void print(String something){
System.out.println(something);
}
};
//use lammbda expression
       //lambda表达式的使用形式是(arg1,arg2,...)->{ 代码块}
        //和上面利用构造函数构造接口实现对象的作用是一样的。但是看起来更加简洁。
Printer printer1= something3->System.out.println(something3); lambda.printSomething(something1, printer);
lambda.printSomething(something2, printer1); }
/*
*
*/
public void printSomething(String something){
System.out.println(something); }
public void printSomething(String something,Printer printer) {
printer.print(something); }
} /*
* function interface
  *这里申明一个函数式接口:只有一个函数的接口
  *Lambda 表达式引入的目标是:Lambda表达式原被定义为一个函数式接口的内联实现。
*/
interface Printer{
public void print(String something); }

  lambda能够写出更加紧凑的代码,便于阅读和跟踪。在性能和多核处理方法上也具有不少好处,详细需要了解stream API.

public class Lambda {
public static void main(String[] args) {
Lambda lambda=new Lambda();String something="oop print somthing";String something1="oop print something2";String something2="lambda print something";
lambda.printSomething(something);Printer printer=new Printer(){@Overridepublic void print(String something){System.out.println(something);}};//use lammbda expressionPrinter printer1= something3->System.out.println(something3);
lambda.printSomething(something1, printer);lambda.printSomething(something2, printer1);}/* *  */public void printSomething(String something){System.out.println(something);}public void printSomething(String something,Printer printer) {printer.print(something);}}
/* * function interface */interface Printer{public void print(String something);}

上一篇:HNCU1101:马的移动(BFS)


下一篇:bash: ./configure: 权限不够 怎么办?