Lambda表達式總結


Lambda表達式


  1. 基礎語法:java8中引入了一個新的操作符“->”,該操作符將lambda表達式拆分為兩部分

    • 左側:Lambda表達式的參數列表,接口中方法的參數列表

    • 右側:Lambda表達式要執行操作,接口的方法重寫的操作

  2. 函數式接口:只有一個抽象方法的接口.lambda表達式需要函數式接口的支持

    • 匿名內部類使用外部類的成員變量,成員變量必須為final

      • 語法格式一、無參數無返回值

        ()->System.out.println("hello world");
      • 語法格式二、一個參數無返回值小括號可以省略

        (a)->System.out.println("hello world");
      • 語法格式三、有返回值有兩個以上的參數lambda體中有多條語句(只有一條語句{}可以省略不寫

        A a1 = (a,b,c,d)->{
           System.out.println(a+b+c+d);
           System.out.println("hello world");
           return new A();
        }
      • 語法格式四、參數列表中的數據類型可以省略不寫,jvm的編譯器可以根據上下文推斷出參數的數據類型(類型推斷)

        四大核心函數是接口

  3. java8四大核心函數式接口

    Consumer<T> :消費型接口

    void accept(T t);

    Supplier<T> :供給型接口

    T get();

    Function<T,R>:函數型接口

    R apply(T t);

    Predicate<T> :斷言型接口

    boolean test(T t);
  4. demo案例

    //lambda表達式
    public class Test01 {
    ​
        // 比較大小,不用lambda表達式
     @Test public void test() { Comparator<Integer> com = new Comparator<Integer>() { @Override public int compare(Integer arg0, Integer arg1) { return Integer.compare(arg0, arg1); } }; ​ } // 用lambda表達式  @Test public void test01() { Comparator<Integer> com = (x, y) -> Integer.compare(x, y); } // 比較employee對象並且 過濾通過年齡 public static List<Employee> compareEmployeesWithAge(List<Employee> emps, Integer age) { List<Employee> li = new ArrayList<>(); for (Employee e : emps) { if (e.getAge() > age) { li.add(e); } } return li; } ​ // 比較employee對象並且 過濾通過工資 public static List<Employee> compareEmployeesWithSalary(List<Employee> emps, Double salary) { List<Employee> li = new ArrayList<>(); for (Employee e : emps) { if (e.getSalary() > salary) { li.add(e); } } return li; } ​ public static List<Employee> compareEmployee(List<Employee> emps ,MyCompare<Employee> mycom){ List<Employee> li = new ArrayList<>(); for (Employee e : emps) { if (mycom.compare(e)) { li.add(e); } } return li; } public static void main(String[] args) { ​ Employee[] emps = new Employee[] { new Employee("張三", 20, 4000.00), new Employee("李四", 21, 5000.00), new Employee("王五", 22, 8000.00), new Employee("趙六", 24, 1000.00), new Employee("呵呵", 30, 9000.00) }; List<Employee> list = Arrays.asList(emps); //不使用lambda表達式 普通的寫法 //System.out.println(compareEmployeesWithAge(list, 20)); //System.out.println(compareEmployeesWithSalary(list, 5000.00)); //不使用lambda表達式 接口實現類(或者匿名內部類)的寫法(策略設計模式) //System.out.println(compareEmployee(list, new MyCompareImpl())); //System.out.println(compareEmployee(list, new MyCompareImpl1())); //System.out.println(compareEmployee(list, (l)->l.getAge() > 20)); list.stream().filter(e->e.getSalary()>5000.00).limit(2).forEach(System.out::println); } } //四大函數接口 public class Test03 { @Test public void test1(){ happy(1000,m->System.out.println("m:"+m)); } public void happy(double money,Consumer<Double> con){ con.accept(money); } @Test public void test2(){ List<Integer> li = getNumber(10,()->(int)Math.random()*100); System.out.println(li); } private List<Integer> getNumber(int num,Supplier<Integer> sup) { List<Integer> list = new ArrayList<>(); for(int i = 0; i < num;i ++){ list.add(sup.get()); } return list; } @Test public void test3(){ String s = strHandler("hello world",(str)->str.trim().toUpperCase()); System.out.println(s); } public String strHandler(String str,Function<String,String> fun){ return fun.apply(str); } @Test public void test4(){ List<String> li = Arrays.asList("hello","world","nihao","shit","nibuhao"); List<String> newli = filterStr(li,s->s.length()>5); System.out.println(newli); } public List<String> filterStr(List<String> list,Predicate<String> pre){ List<String> strList = new ArrayList<>(); for(String s : list){ if(pre.test(s)){ strList.add(s); } } return strList; } } //方法引用 public class Test04 { @Test public void Test1(){ //等價於 Consumer<String> con = (x)->System.out.println(x); Consumer<String> con1 = System.out::print; // Consumer<String> con2 = new Consumer<String>() { // @Override // public void accept(String s) { // System.out.println(s); // } // }; //等價於 con.accept("hello"); con1.accept("hello"); } @Test public void test2(){ Employee emp = new Employee("mihao",122,121.00); Supplier<String> sup = emp::getName; Supplier<String> sup1 = ()->emp.getName(); System.out.println(sup.get()); System.out.println(sup1.get()); } @Test public void comparator(){ Comparator<Integer> com = Integer::compare; Comparator<Integer> com1 = (x,y)->Integer.compare(x,y); com.compare(1, 2); com1.compare(1, 2); } @Test public void test4(){ //返回值boolean 類::實例方法 BiPredicate<String,String> bp = String::equals; //滿足條件第一個參數是調用者 BiPredicate<String,String> bp1 = (x,y)->x.equals(y); } @Test public void test5(){ Supplier<Employee> sup = Employee::new; Supplier<Employee> sup1 = ()->new Employee(); sup.get(); } @Test public void test6() { Function<Integer, String[]> fun = String[]::new; Function<Integer,String[]> fun1 = (x)->new String[x]; fun.apply(10); } }

     

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM