Java--8--新特性--Lambda
阿新 • • 發佈:2017-11-27
value 需要 員工信息 span final oid function get test
java9 都出來了,我才開始接觸到java8的新特性,有點脫節啊。。
Lambda是一個匿名函數,可以理解為一段可以傳遞的代碼,將代碼像數據一樣傳遞,下面是一個小例子。
public class Employee { private String name; private int age; private int salary; //getter and setter } //----------------------------------------------- public interface MyInter { Boolean filter(Employee list); }//----------------------------------------------- public class MyInterImpl implements MyInter { @Override public Boolean filter(Employee list) { return list.getage()>35; } } //----------------------------------------------- package LambdaP; import org.junit.Test; import java.util.ArrayList;import java.util.Arrays; import java.util.List; public class LamBdaDemo { public static final List<Employee> employees = Arrays.asList( new Employee("張三", 19, 2000), new Employee("張四", 33, 3000), new Employee("張五", 38, 4000), new Employee("張六", 41, 2500),new Employee("張七", 42, 5500) ); //需求:當前公司中員工的年冷大於三十五的員工信息 //傳統方法 public List<Employee> getEmployees() { List<Employee> list = new ArrayList<>(); for (Employee employee : employees) { if (employee.getage() > 35) { list.add(employee); } } return list; } public void test1() { List<Employee> employees = this.getEmployees(); for (Employee e : employees) { System.out.println(e); } } //方法2 public List<Employee> filterEmployee(List<Employee> list,MyInter inter){ List<Employee> newlist = new ArrayList<>(); for (Employee employee : list) { if(employee.getage()>35){ newlist.add(employee); } } return newlist; } public void test2(){ List<Employee> employees = this.filterEmployee(LamBdaDemo.employees, new MyInterImpl()); for (Employee employee : employees) { System.out.println(employee); } } //Lambda @Test public void test3(){ List<Employee> employees = this.filterEmployee(LamBdaDemo.employees, (e) -> e.getage() > 35); for (Employee employee : employees) { System.out.println(employee); } System.out.println("Lambda ....."); } //方法三:如果以上的都不存在的話 Stream API 也是在java8中新更新的 @Test public void test4(){ employees.stream().filter((e)->e.getage()>35).forEach(System.out::println); } }
以上就能看出來Lambda的便捷性,下面開始我自己的學習筆記
import org.junit.Test; import java.util.Comparator; import java.util.function.Consumer; public class LamBdaDemo2 { /** * Lambda的基本語法:java8中新引入了“->”操作符,簡稱箭頭操作符或者Lambda操作符, * 他將Lambda表達式拆分成兩部分 * 1. 左側:對應Lambda的參數列表 * 2. 右側:Lambda所要執行的功能,”Lambda體“ * 箭頭操作符的左側對應接口中的參數列表,右側即所要實現的方法。 * JDK 1.7 之前內部類引用同級的外部變量必須加final,現在不用手動加,默認,但是還是在內部類中不能操作外部變量,Lambda中同樣適用。 * 語法格式一:無參數,無返回值 test1 * ()-> System.out.println("Hello"); * 語法格式二:有一個參數,無返回值 test2 * (e)-> { 實現 }; * 語法格式三:有兩個以上個參數,並且Lambda體有多條語句,有返回值。 test3 * (x,y)->{ 實現 }; * 語法格式四:Lambda 表達式的參數列表類型可以省略不寫,如果寫就必須全部寫,JVM可以自己推斷出參數類型。 * Comparator<Integer> comparator2 = (Integer x,Integer y) -> Integer.compare(x,y); * * 左右遇一括號省 :即Lambda體重左右兩邊如果只有一個參數或者一條語句的話括號可以省略不寫。 * 左側推斷類型省 :JVM自己推斷出來 * * Lambda表達式需要“函數式接口的支持” * 函數式接口:若接口中只有一個抽象方法的接口叫做函數式接口 * 用Ctrl加左擊點擊接口名可以查看系統提供的接口是否為函數式接口 * 系統提供的函數式接口都加有 @FunctionalInterface 註解 * 此註解可以檢查是否是函數式接口 */ public void test1(){ Runnable runnable1 = new Runnable() { @Override public void run() { System.out.println("Hello"); } }; runnable1.run(); System.out.println("*********************"); Runnable runnable = ()-> System.out.println("Hello Lambda"); runnable.run(); } public void test2(){ //consumer 定義 @FunctionalInterface // public interface Consumer<T> { //Consumer 中的方法 void accept(T t); Consumer<String> consumer = (e) -> System.out.println("Lambda " + e); consumer.accept("Hello"); //在只有一個參數的情況下小括號是可以省略不寫的 Consumer<String> con = e -> System.out.println("Lambda " + e); con.accept("Hello"); } public void test3(){ Comparator<Integer> comparator = (x,y) ->{ System.out.println("比較x y"); return Integer.compare(x,y); }; System.out.println(comparator.compare(1,2)); System.out.println("**************************"); //如果如上只有一條語句後面的大括號 可以省略不寫 Comparator<Integer> comparator2 = (x,y) -> Integer.compare(x,y); int compare = comparator2.compare(2, 1); System.out.println(compare); } }
下面來實現一個小功能:對兩個數的任意操作
@FunctionalInterface public interface MyInter2 { Integer getValue(Integer x); }
Java--8--新特性--Lambda