java lambda 的用法

一.打印数组

String[] s = "fdsfsdfds".split("");     
Stream<String> str = Stream.of(s);
str.forEach(d->System.out.print(d + " "));//编译器可以自动推断d的类型
 //output: f d s f s d f d s 

二.方法引用

利用方法引用 上面的代码也可以被写成如下形式

str.forEach(System.out::print);//方法引用,System.out::print等价于x -> System.out.println(x)
//output: fdsfsdfds

 三. 接口

public class Text{
public static void main(String[] args)
{
Tests tsd = ()->"fdsadfsds"; //接口只能有一个函数
System.out.println(tsd.print());
//output: fdsadfsds
}
}
interface Tests{
public String print();
}

 

posted @ 2019-01-26 02:59  江期玉  阅读(982)  评论(0编辑  收藏  举报