本文共 3626 字,大约阅读时间需要 12 分钟。
安装JDK8;
直接安装,并修改环境变量“JAVA_HOME”为相应文件夹设置eclipse的JRE为JDK8下的jre;
“Window”->”Reference”->”Java”->”Installed JREs”->添加默认jre(注意以往的项目还是基于原默认的JRE需手动修改为当前JRE)安装“JAVA8 support for Eclipse keplerSR2”插件
“Help”->”Eclipse Marketplace…”->输入“JAVA 8 Kelper”搜索->安装插件。import java.util.Arrays;import java.util.List;import java.util.function.Predicate;public class Main { public static void main(String []args){ Listlanguages = Arrays.asList("Java", "Scala", "C++", "Haskela", "Lisp"); System.out.println("Languages which starts with J :"); filter(languages, (str)->((String) str).startsWith("J")); System.out.println("Languages which ends with a "); filter(languages, (str)->((String) str).endsWith("a")); System.out.println("Print all languages :"); filter(languages, (str)->true); System.out.println("Print no language : "); filter(languages, (str)->false); System.out.println("Print language whose length greater than 4:"); filter(languages, (str)->((String) str).length() > 4); //注意Stream用法, List lowercaseNames = languages.stream().map(name -> name.toLowerCase()).collect(Collectors.toList()); System.out.println("Print language in lowercase:"); for(String str:lowercaseNames) { System.out.println(str); } } @SuppressWarnings("unchecked")//忽视IDE提示的警告 public static void filter(List names, @SuppressWarnings("rawtypes") Predicate condition) { for(String name: names) { if(condition.test(name)) { System.out.println(name + " "); } } }}
Lambda 理解:一段带有输入参数的可执行语句块。实质是一种,是声明式编程()
(维基百科解释:a function (or a subroutine) defined, and possibly called, without being bound to an identifier。)lambda表达式的一般语法:是一种匿名方法,参考文档
//一般省略参数类型,编译器能够根据上下文自动识别参数类型//只有一个参数可以省略小括号//只有一句代码可以省略大括号//lambda访问的外部变量不可变,java的匿名内部类在访问外部变量的时候,外部变量必须用final修饰,lambda编译器隐式转换成final来处理//lambda中this是指声明它的外部对象//(Type1 param1, Type2 param2, ..., TypeN paramN) -> { statment1; statment2; //............. return statmentM;}
3.lambda的函数调用,必须基于接口。
public class Main { interface HelloService { String hello(String firstname, String lastname); } public static void main(String[] args) { HelloService helloService=(String firstname, String lastname) -> { String hello="Hello " + firstname + " " + lastname; return hello; }; System.out.println(helloService.hello(args[0], args[1])); } }
interface WorkflowManager{ public default void doTask1(){ System.out.println("Doing Task1..."); } public void doTask2(); public default void doTask3(){ System.out.println("Doing Task3..."); } public default void doTask4(){ System.out.println("Doing Task4..."); }}public class Main { public static void main(String[] args) { /** * Using lambda expression to create different * implementation of the abstract workflow */ initiateWorkFlow(()->System.out.println("Doing Task2.1...")); initiateWorkFlow(()->System.out.println("Doing Task2.2...")); initiateWorkFlow(()->System.out.println("Doing Task2.3...")); } static void initiateWorkFlow(WorkflowManager workflowMgr){ System.out.println("Starting the workflow ..."); workflowMgr.doTask1(); workflowMgr.doTask2(); workflowMgr.doTask3(); workflowMgr.doTask4(); } }
参考资料:
转载地址:http://lrkab.baihongyu.com/