LoginSignup
30
33

More than 5 years have passed since last update.

Java8関数型インターフェース チートシート

Posted at

Java8でLambdaを書く際によく使用する関数型インターフェースのほとんどは、java.util.functionに用意されています。

しかし、どのシチュエーションで、そのインターフェースを使えば良いか最初はわかりにくいです。
なので、引数の型や戻り値などから使うべきインターフェースを、逆引きで一覧にしてみました。

  • Function系

引数が1つまたは2つあり、戻り値があるパターン

1つ目の引数の型 2つ目の引数の型 戻り値の型 インターフェース名 備考
指定 - 指定 Function
Integer - 指定 IntFunction
Long - 指定 LongFunction
Double - 指定 DoubleFunction
指定 指定 指定 BiFunction
指定 - Integer ToIntFunction
指定 - Long ToLongFunction
指定 - Double ToDoubleFunction
指定 指定 Integer ToIntBiFunction
指定 指定 Long ToLongBiFunction
指定 指定 Double ToDoubleBiFunction
Integer - Long IntToLongFunction
Integer - Double IntToDoubleFunction
Long - Integer LongToIntFunction
Long - Double LongToDoubleunction
Double - Integer DoubleToIntFunction
Double - Long DoubleToIntFunction
  • Consumer系

引数が1つまたは2つあり、戻り値がないパターン

1つ目の引数の型 2つ目の引数の型 戻り値の型 インターフェース名 備考
指定 - void Consumer
Integer - void IntConsumer
Long - void LongConsumer
Double - void DoubleConsumer
指定 指定 void BiConsumer
指定 Integer void ObjIntConsumer
指定 Long void ObjLongConsumer
指定 Double void ObjDoubleConsumer
  • Supplier系

引数が無く、戻り値があるパターン

1つ目の引数の型 2つ目の引数の型 戻り値の型 インターフェース名 備考
- - 指定 Supplier
- - Integer IntSupplier
- - Long LongSupplier
- - Double DoubleSupplier
- - boolean BooleanSupplier
  • Predicate系

引数が1つまたは2つあり、戻り値がbolean型のパターン

1つ目の引数の型 2つ目の引数の型 戻り値の型 インターフェース名 備考
指定 - boolean Predicate
指定 指定 boolean BiPredicate
Integer - boolean IntPredicate
Long - boolean LongPredicate
Double - boolean DoublePredicate
  • UnaryOperator系

1つ目の引数と戻り値の型が同じパターン

1つ目の引数の型 2つ目の引数の型 戻り値の型 インターフェース名 備考
指定 - 1つ目の引数と同じ UnaryOperator
Integer - Integer IntUnaryOperator
Long - Long LongUnaryOperator
Double - Double DoubleUnaryOperator
30
33
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
33