概要
Function <T, R>
apply(T)を関数メソッドに持つ関数型インタフェースです。
1つの引数を受け取って結果(R)を返します。
基本的な使い方
Functionインターフェースの第1の型(T)にはapplyメソッドの引数になる型を指定します。
第2の型(R)には処理結果の戻り値の型を指定します。
以下では、StringをIntegerに変換した結果を返しています。
1 2 3 4 5 6 7 |
// apply Function<String, Integer> parseStrToInt = str -> Integer.valueOf(str); System.out.println(parseStrToInt.apply("100").getClass()); // class java.lang.Integer |
各メソッドの使い方
andThen(Function after)
まず自身の関数を適用してから、関数afterを適用した結果を返します。
1 2 3 4 5 6 7 8 |
// andThen Function<String, Integer> parseStrToInt = str -> Integer.valueOf(str); Function<Integer, Long> calcTaxed = num -> Math.round(num * 1.1); System.out.println(parseStrToInt.andThen(calcTaxed).apply("100")); // 110 |
compose(Function before)
まず関数beforeを適用してから、自身を適用した結果を返します。
1 2 3 4 5 6 7 8 |
// compose Function<Integer, Long> calcTaxed = num -> Math.round(num * 1.1); Function<Long, String> parseLongToStrt = num -> String.valueOf(num); System.out.println(parseLongToStrt.compose(calcTaxed).apply(100)); // 110 |
identity()
常に入力引数を返す関数を返します。
これはstaticなメソッドです。
1 2 3 4 5 6 7 8 9 10 |
// identity System.out.println(Function.identity()); System.out.println(Function.identity().apply(100)); System.out.println(Function.identity().apply("String")); // java.util.function.Function$$Lambda$24/0x000000010008f440@58ceff1 // 100 // String |
Functionの特殊化された他のインターフェース
引数2つを持つように特殊化されたインターフェース
BiFunction<T,U,R>
2つの引数を受け取って結果を生成して返します。
T – 関数の第1引数の型
U – 関数の第2引数の型
R – 関数の結果の型
1 2 3 4 5 6 7 8 |
// BiFunction BiFunction<Integer, Integer, String> multiplicationText = (x, y) -> String.valueOf(x + " * " + y + " = " + x * y); System.out.println(multiplicationText.apply(9, 9)); // 9 * 9 = 81 |
ToIntBiFunction<T,U,R>
2つの引数を受け取ってint値の結果を返す関数を生成します。
T – 関数の第1引数の型
U – 関数の第2引数の型
applyAsInt(Object, Object)をメソッドとして持ちます。
1 2 3 4 5 6 7 8 |
// ToIntBiFunction ToIntBiFunction<String, String> plusStrToInt = (x, y) -> Integer.valueOf(x) + Integer.valueOf(y); System.out.println(plusStrToInt.applyAsInt("10", "20")); // 30 |
ToDoubleBiFunction<T,U,R>
2つの引数を受け取ってdouble値の結果を返す関数を生成します。
T – 関数の第1引数の型
U – 関数の第2引数の型
applyAsDouble(Object, Object)メソッドの持ちます。
※基本的な操作はToIntBiFunctionと同等なのでソースコードは割愛。
ToLongBiFunction<T,U,R>
2つの引数を受け取ってlong値の結果を返す関数を生成します。
T – 関数の第1引数の型
U – 関数の第2引数の型
applyAsLong(Object, Object)をメソッドとして持ちます。
※基本的な操作はToIntBiFunctionと同等なのでソースコードは割愛。
プリミティブ特殊型
プリミティブ型を引数にしてR型の結果を返します。
IntFunction<R>
intを引数にしてR型の結果を返します。
1 2 3 4 5 6 7 8 9 |
// IntFunction IntFunction<String> intToStr = num -> String.valueOf(num); System.out.println(intToStr.apply(100).getClass()); System.out.println(intToStr.apply(100)); // class java.lang.String // 100 |
DoubleFunction<R>
doubleを引数にしてR型の結果を返します。
※基本的な操作はIntFunctionと同等なのでソースコードは割愛。
LongFunction<R>
longを引数にしてR型の結果を返します。
※基本的な操作はIntFunctionと同等なのでソースコードは割愛。
プリミティブ特殊型(変換)
関数名の左辺を引数として、右辺の値に変換した結果を返します。
IntToDoubleFunction
int値を引数として、double値を返します。
applyAsDouble(int)をメソッドとして持ちます。
1 2 3 4 5 6 7 |
// IntToDoubleFunction IntToDoubleFunction calcTaxed = num -> num * 1.1; System.out.println(calcTaxed.applyAsDouble(100)); // 110.00000000000001 |
他にも以下のインターフェースが用意されており、
基本的な使い方は同じです。
- IntToLongFunction
- DoubleToIntFunction
- DoubleToLongFunction
- LongToIntFunction
- LongToDoubleFunction
プリミティブ特殊型(生成)
プリミティブ値の結果を生成した結果を返します。
ToIntFunction<T>
int値の結果を生成した結果を返します。
T – 関数の入力の型
applyAsInt(Object)をメソッドとして持ちます。
1 2 3 4 5 6 7 8 9 |
// ToIntFunction ToIntFunction<Boolean> flgValue = bool -> bool == true ? 1 :0; System.out.println(flgValue.applyAsInt(true)); System.out.println(flgValue.applyAsInt(false)); // 1 // 0 |
他にも以下のインターフェースが用意されており、
基本的な使い方は同じです。
- ToDoubleFunction
- ToLongFunction
まとめ
- Functionは引数を1つ持ち、処理結果を返す関数型インターフェースである。
- 特殊型として、2つの引数を持つBi、プリミティブ型を使用するインターフェース、プリミティブ型を変換するインターフェース、プリミティブ型を生成するインターフェースがある。
最後に
独学が難しいと感じていたり、エラーの解消に時間がかかるようだったらオンラインのサポートサービスを検討してみて下さい。
回答率100%の掲示板に質問し放題のオンラインサービス
参考情報
インタフェースFunction<T,R>
パッケージjava.util.function