Java : Function - API使用例
Function (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
Function は、パラメータあり、戻り値ありの関数型インタフェースです。
主に、Stream や Optional の ラムダ式 として使われます。
final var stream = Stream.of("0x1", "0xa", "0xff", null);
final var func = new Function<String, Integer>() {
@Override
public Integer apply(String s) {
if (s == null) {
return null;
}
return Integer.decode(s) * 10;
}
};
final var ret = stream.map(func).toList();
System.out.println(ret); // [10, 100, 2550, null]
// ラムダ式の例です。
final var stream = Stream.of("0x1", "0xa", "0xff", null);
final var ret = stream.map(s -> {
if (s == null) {
return null;
}
return Integer.decode(s) * 10;
}).toList();
System.out.println(ret); // [10, 100, 2550, null]
メソッド
default <V> Function<T,V> andThen (Function<? super R,? extends V> 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) {
System.out.println(" func1 apply!");
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) {
System.out.println(" after apply!");
return s.toUpperCase();
}
};
final var func2 = func1.andThen(after);
try {
System.out.println("-- apply null --");
func2.apply(null);
} catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException! : " + e.getMessage());
}
// 結果
// ↓
//-- apply null --
// func1 apply!
//IllegalArgumentException! : s is null
R apply (T t)
final var stream = Stream.of("0x1", "0xa", "0xff", null);
final var func = new Function<String, Integer>() {
@Override
public Integer apply(String s) {
if (s == null) {
return null;
}
return Integer.decode(s) * 10;
}
};
final var ret = stream.map(func).toList();
System.out.println(ret); // [10, 100, 2550, null]
// ラムダ式の例です。
final var stream = Stream.of("0x1", "0xa", "0xff", null);
final var ret = stream.map(s -> {
if (s == null) {
return null;
}
return Integer.decode(s) * 10;
}).toList();
System.out.println(ret); // [10, 100, 2550, null]
default <V> Function<V,R> compose (Function<? super V,? extends T> 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) {
System.out.println(" before apply!");
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) {
System.out.println(" func1 apply!");
return s.toUpperCase();
}
};
final var func2 = func1.compose(before);
try {
System.out.println("-- apply null --");
func2.apply(null);
} catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException! : " + e.getMessage());
}
// 結果
// ↓
//-- apply null --
// before apply!
//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
関連記事
- API 使用例
- @FunctionalInterface
- BiConsumer
- BiFunction
- BiPredicate
- BooleanSupplier
- Comparator (比較)
- Consumer
- DoubleConsumer
- DoubleFunction
- DoublePredicate
- DoubleSupplier
- IntConsumer
- IntFunction
- IntPredicate
- IntSupplier
- LongConsumer
- LongFunction
- LongPredicate
- LongSupplier
- Predicate
- Runnable
- Supplier
- ToIntFunction