ラムダのための一般的な方法を記述する方法?

滝:

私は、次のインタフェースを持っています:

public interface Mapper {
    public <T> T map(T element);
}

そして私が行うときMapper mapper = (int n) -> n*2;、私は問題を取得します:

不正なラムダ式:タイプマッパーのメソッドマップが一般的であり

私はここで何をしないのですか?それはどのようにラムダ式で使用する一般的な方法を作成することは可能でしょうか?

また:

あなたはにあなたの定義を変更する必要があり

public interface Mapper<T> { // type bound to the interface
    T map(T element);
}

そして、それをとして使用します。

Mapper<Integer> mapper = element -> element * 2; // notice Integer and not 'int' for the type

以下のように書くこともできます。

Mapper<Integer> mapper = (Integer element) -> element * 2;

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=211408&siteId=1
おすすめ