Java 8 lambda初试

栏目: 编程语言 · Java · 发布时间: 8年前

内容简介:Java 8 lambda初试

λ表达式本质上是一个匿名方法。让我们来看下面这个例子:

public int add(int x, int y) {
    return x + y;
}

转成λ表达式后是这个样子:

(int x, int y) -> x + y;

参数类型也可以省略,Java编译器会根据上下文推断出来:

(x, y) -> x + y; //返回两数之和

或者

(x, y) -> { return x + y; } //显式指明返回值

可见λ表达式有三部分组成:参数列表,箭头(->),以及一个表达式或语句块。

下面这个例子里的λ表达式没有参数,也没有返回值(相当于一个方法接受0个参数,返回void,其实就是Runnable里run方法的一个实现):

() -> { System.out.println("Hello Lambda!"); }

如果只有一个参数且可以被 Java 推断出类型,那么参数列表的括号也可以省略:

c -> { return c.size(); }
public static void main(String[] args) {
        Arrays.asList( "a", "b", "d" ).forEach( e -> {
            System.out.print( e +"\n");

        } );
        System.out.print( "\n--------------------------" );
        Arrays.asList( "a", "b", "d" ).sort( ( e1, e2 ) -> e1.compareTo( e2 ) );


        /**
         * 可见λ表达式有三部分组成:参数列表,箭头(->),以及一个表达式或语句块。
         * 下面这个例子里的λ表达式没有参数,也没有返回值(相当于一个方法接受0个参数,返回void,其实就是Runnable里run方法的一个实现):
         * () -> { System.out.println("Hello Lambda!"); }
         */
        Thread t2=new Thread(()->{
            System.out.println("This is from an anonymous method (lambda exp).\n");
        });
        t2.start();
        /**
         * 迭代LIST
         */
        List<String> listStr=new ArrayList<>();
        listStr.add("sss");
        listStr.add("1111");
        listStr.forEach(e->{
            if(e.equals("sss")){
                System.out.print(e);
            }
        });

    }

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Clean Architecture

Clean Architecture

Robert C. Martin / Prentice Hall / 2017-9-20 / USD 34.99

Practical Software Architecture Solutions from the Legendary Robert C. Martin (“Uncle Bob”) By applying universal rules of software architecture, you can dramatically improve developer producti......一起来看看 《Clean Architecture》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具