Java : Function - API使用例
Function (Java SE 18 & JDK 18) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
1つの引数を受け取って結果を生成する関数を表します。
Function は、パラメータあり、戻り値ありの関数型インタフェースです。
主に、Stream や Optional のラムダ式として使われます。
final var stream = Stream.of("0x1", "0xa", "0xff");
final var func = new Function<String, Integer>() {
@Override
public Integer apply(String s) {
return Integer.decode(s) * 10;
}
};
final var ret = stream.map(func).toList();
System.out.println(ret); // [10, 100, 2550]
// ラムダ式の例です。
final var stream = Stream.of("0x1", "0xa", "0xff");
final var ret = stream.map(s -> Integer.decode(s) * 10).toList();
System.out.println(ret); // [10, 100, 2550]
メソッド
default <V> Function<T,V> andThen (Function<? super R,? extends V> after)
まず入力にこの関数を適用し、次に結果に関数afterを適用する合成関数を返します。
final var func1 = new Function<String, String>() {
@Override
public String apply(String s) {
return s + "-xyz";
}
};
final var after = new Function<String, String>() {
@Override
public String apply(String s) {
return s.toUpperCase();
}
};
System.out.println(func1.apply("abcd")); // "abcd-xyz"
final var func2 = func1.andThen(after);
System.out.println(func2.apply("abcd")); // "ABCD-XYZ"
final var func1 = new Function<String, String>() {
@Override
public String apply(String s) {
if (s == null) {
throw new IllegalArgumentException("s is null");
}
return s + "-xyz";
}
};
final var after = new Function<String, String>() {
@Override
public String apply(String s) {
// ★ここは実行されません。
return s.toUpperCase();
}
};
try {
final var func2 = func1.andThen(after);
func2.apply(null);
} catch (IllegalArgumentException e) {
System.out.println(e);
}
// 結果
// ↓
//java.lang.IllegalArgumentException: s is null
R apply (T t)
指定された引数にこの関数を適用します。
final var func = new Function<String, Integer>() {
@Override
public Integer apply(String s) {
return Integer.decode(s) * 10;
}
};
final var ret = func.apply("0xff");
System.out.println(ret); // 2550
// ラムダ式の例です。
final var stream = Stream.of("0x1", "0xa", "0xff");
final var ret = stream.map(s -> Integer.decode(s) * 10).toList();
System.out.println(ret); // [10, 100, 2550]
default <V> Function<V,R> compose (Function<? super V,? extends T> before)
まず入力に関数beforeを適用し、次に結果にこの関数を適用する合成関数を返します。
final var before = new Function<String, String>() {
@Override
public String apply(String s) {
return "abc-" + s;
}
};
final var func1 = new Function<String, String>() {
@Override
public String apply(String s) {
return s.toUpperCase();
}
};
System.out.println(func1.apply("xyz")); // "XYZ"
final var func2 = func1.compose(before);
System.out.println(func2.apply("xyz")); // "ABC-XYZ"
final var before = new Function<String, String>() {
@Override
public String apply(String s) {
if (s == null) {
throw new IllegalArgumentException("s is null");
}
return "abc-" + s;
}
};
final var func1 = new Function<String, String>() {
@Override
public String apply(String s) {
// ★ここは実行されません。
return s.toUpperCase();
}
};
try {
final var func2 = func1.compose(before);
func2.apply(null);
} catch (IllegalArgumentException e) {
System.out.println(e);
}
// 結果
// ↓
//java.lang.IllegalArgumentException: s is null
static <T> Function<T,T> identity ()
常に入力引数を返す関数を返します。
final var func1 = Function.<String>identity();
System.out.println(func1.apply("xyz")); // xyz
final var func2 = Function.<Integer>identity();
System.out.println(func2.apply(1234)); // 1234