Java_ランバー表現

4320 ワード

Lambada表式:((Apple a 1,Apple a 2)-』{a 1.get Weight().compreTo(a 2.get Weight)}}パラメータリスト――ここではCompratorにおけるcomppare方法のパラメータを採用しています。矢印->'はパラメータリストをLambada本体から分離します。Lambada本体——二つのAppleの重さを比較します。表式はLamdaの戻り値です。
①関数式インターフェース
public interface Predicate{
    boolean test (T t);
}

public interface Comparator {
    int compare(T o1, T o2);
}
public interface Runnable{
    void run();
}
public interface ActionListener extends EventListener {
    void actionPerformed(ActionEvent e);
}
public interface Callable{
    V call();
}
public interface PrivilegedAction{
    V run();
}
②折り返し実行モード
//          。         
public static String processFile() throws IOException {
     //     try  ,          
    try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
        return br.readLine();
        }
}

//             
public interface BufferedReaderProcessor {
    String process(BufferedReader b) throws IOException;
}

//       
public static String processFile(BufferedReaderProcessor p) throws IOException {
    try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
        return p.process(br);
    }
}

//   Lambda
//     
// String oneLine = processFile((BufferedReader br) -> br.readLine());
//     :
// String twoLines = processFile((BufferedReader br) -> br.readLine() + br.readLine());
③例:Function
//            T,R;apply      T,      R
public interface Function{
    R apply(T t);
}

public static  List map(List list, Function f) {
    List result = new ArrayList();
    for(T s: list){
        result.add(f.apply(s));
    }
    return result;
}
// [7, 2, 6]
// List l = map( Arrays.asList("lambdas","in","action"), (String s) -> s.length());
④ローカル変数Lambda表式を使うと、匿名クラスのように自由変数(パラメータではなく、外層作用領域で定義される変数)も使用できます。これらはLambada捕獲と呼ばれるが、局所変数は明示的にfinalとして宣言しなければならない。
int portNumber = 1337;
// Runnable r = () -> System.out.println(portNumber);

//          ,  portNumber       
int portNumber = 1337;
Runnable r = () -> System.out.println(portNumber);
portNumber = 31337;
⑤方法引用方法の参照が必要な場合は、ターゲット参照はセパレータに配置されます。例えば、Apple::get Weightは、Appleクラスで定義されている方法を引用したget Weightです。括弧は必要ありません。実際にこの方法を呼び出していません。方法引用とは、Lamboda式(Apple a)->a.get Weight()のショートカットの書き方です。方法を引用することができます。単一の方法だけを扱うLambandのシンタックスキャンディーに対して、同じことを表現する時に書くコードがもっと少ないからです。
List str = Arrays.asList("a","b","A","B");
str.sort((s1, s2) -> s1.compareToIgnoreCase(s2));
str.sort(String::compareToIgnoreCase);
⑥コンストラクタの引用は既存のコンストラクタに対して、その名称とキーワードnewを利用してその引用を作成できます。Class Name:new。その機能は静的方法を指す参照と似ている。
//           Apple Lambda   
// Supplier c1 = () -> new Apple();
//            Apple()    
// Supplier c1 = Apple::new;

//             Apple(Integer weight),          Apple Lambda   
// Function c2 = (weight) -> new Apple(weight);
//   Apple(Integer weight)       
// Function c2 = Apple::new;
⑦Lamberと方法は実戦を引用する
//  1  :    
public class AppleComparator implements Comparator {
    public int compare(Apple a1, Apple a2){
        return a1.getWeight().compareTo(a2.getWeight());
    }
}
inventory.sort(new AppleComparator());

//  2  :     
inventory.sort(new Comparator() {
    public int compare(Apple a1, Apple a2){
        return a1.getWeight().compareTo(a2.getWeight());
    }
});

//  3  :  Lambda    
inventory.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));

// Java       Lambda         Lambda        。           :
//    inventory.sort((a1, a2) -> a1.getWeight().compareTo(a2.getWeight()));
//    Comparator      comparing       ,       Function   Comparable  ,     Comparator  
//    Comparator c = Comparator.comparing((Apple a) -> a.getWeight());
//                 
//    import static java.util.Comparator.comparing;
//    inventory.sort(comparing((a) -> a.getWeight()));

//  4  :      
//    inventory.sort(comparing(Apple::getWeight));