JAVA8

函數式接口 在接口中增長抽象方法來實現函數式接口java

  • 在一個接口中定義一個惟一的抽象方法,那麼這個接口就是函數式接口
  • 經過註解@FunctionalInterface
package com.lsq.java8;

/**
 * Created by lsq on 2017/1/20.
 * 接口只能定義一個抽象方法
 */
@FunctionalInterface
public interface FunctuonInterfaceTest {
    String sayHello(String name);
}
package com.lsq.java8;

/**
 * Created by lsq on 2017/1/20.
 */
public class AppDemo {

    public static void main(String[] args) {
        FunctuonInterfaceTest demo = (name)-> {
            return "Hello World :dome-"+name;
        };
        System.out.println(demo.sayHello("lsq"));
    }
}
  • 函數式接口容許定義靜態方法
package com.lsq.java8;

/**
 * Created by lsq on 2017/1/20.
 * 接口只能定義一個抽象方法
 */
@FunctionalInterface
public interface FunctuonInterfaceTest {

    String sayHello(String name);
    //java 8 函數式接口容許定義靜態方法
    static void  doSomething(){
        System.out.println("java 8 函數式接口容許定義靜態方法");
    }
}
  • 方法的引用
  1. 引用靜態方法
  2. 引用對象的實例方法
  3. 引用某個類型的任意對象的實例方法
  4. 引用類構造方法
package com.lsq.java8.lambdaDemo;

/**
 * Created by lsq on 2017/1/20.
 */
public class Persion implements Comparable<Persion>{
    private String name;

    public Persion() {
    }

    public Persion(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int comp2(Persion p1,Persion p2){
        return  p1.getName().compareTo(p2.getName());
    }
    @Override
    public int compareTo(Persion o) {
        return o.getName().compareTo(this.getName());
    }
}
package com.lsq.java8.lambdaDemo;

import java.util.function.Supplier;

/**
 * Created by lsq on 2017/1/20.
 */
public class PersionFactory {

    private  Supplier<Persion> supplier;

    public PersionFactory(Supplier<Persion> supplier) {
        this.supplier = supplier;
    }
    public Persion getPersion(){
        return supplier.get();
    }
}
package com.lsq.java8.lambdaDemo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lsq on 2017/1/20.
 */
public class AppDemo {
    static List<Persion> ps = new ArrayList<Persion>(){
        {
            add(new Persion("aa"));
            add(new Persion("hh"));
            add(new Persion("vv"));
            add(new Persion("cc"));
            add(new Persion("dd"));

        }
    };



    public static void main(String[] args) {
      PersionFactory factory = new PersionFactory(Persion::new ); //引用類構造
        print();
        ps.sort(AppDemo::myCompare);   // 引用靜態方法
        print();
        ps.sort(ps.get(0) ::comp2);   //引用對象的實例方法
        print();
        ps.sort(Persion::compareTo); //某個類型的任意對象的實例方法
    }

    private static void print(){
        ps.forEach((Persion persion) -> {
            System.out.println(persion.getName() +" ");
        });
        System.out.println("_________________________________");

    }

    private static int myCompare(Persion p1,Persion p2){
        return p1.getName().compareTo(p2.getName());
    }

}
//容許定義默認方法
    default  void doDefaultSomething(){
        System.out.println("doSomething");
    }
相關文章
相關標籤/搜索