71
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

関数型インターフェースを利用したラムダ式の使用例まとめ(Java Gold資格勉強)

Last updated at Posted at 2025-12-06

1.はじめに

Java Goldの資格勉強をしている時に出てきた
関数型インターフェースとラムダ式についての内容が、
種類が多く個人的に把握することが大変だと感じたため、
代表的なものを使用例と合わせて記載しようと思います。

2.関数型インターフェースとは

「定義されている抽象メソッドが1つだけのインタフェース」のことです。
"@FunctionalInterface"アノテーションを使用することで、
意図的に関数型インターフェイスとして定義でき、
複数の抽象メソッドを持たないことをコンパイラが保証してくれます。

また、関数型インターフェースはラムダ式の記述に利用することができます。
Javaには標準でいくつかの関数型インターフェースが用意されており、
この記事では代表的な関数型インターフェースの種類と、
それぞれを利用したラムダ式の使用例をまとめていきます。

3.代表的な関数型インターフェース

インターフェース 内容 メソッド
Supplier<T> 引数無し、T型の結果を返す get()
Consumer<T> T型の引数1つ、結果を返さない accept(T)
BiConsumer<T,U> T型とU型の引数2つ、結果を返さない accept(T, U)
Function<T,R> T型の引数1つ、R型の結果を返す apply(T)
BiFunction<T,U,R> T型とU型の引数2つ、R型の結果を返す apply(T, U)
Predicate<T> T型の引数1つ、boolean型の結果を返す test(T)
BiPredicate<T,U> T型とU型の引数2つ、boolean型の結果を返す test(T, U)
UnaryOperator<T> T型の引数1つ、同じT型の結果を返す apply(T)
BinaryOperator<T> T型の引数2つ、同じT型の結果を返す apply(T, T)
Runnable 引数無し、結果を返さない run()
Callable<V> 引数無し、V型の結果を返す(例外をスローできる) call()

4.使用例

・Supplier<T>

引数無し、T型の結果を返す

Sample.java
Supplier<String> supplier = () -> "Hello, World!";
System.out.println(supplier.get());  
// 出力: Hello, World!

・Consumer<T>

T型の引数1つ、結果を返さない

Sample.java
Consumer<String> consumer = s -> System.out.println("Consumed: " + s);
consumer.accept("Hello");  
// 出力: Consumed: Hello

・BiConsumer<T,U>

T型とU型の引数2つ、結果を返さない

Sample.java
BiConsumer<String, Integer> biConsumer = (name, age) -> System.out.println(name + " is " + age + " years old.");
biConsumer.accept("Alice", 30);  
// 出力: Alice is 30 years old.

・Function<T,R>

T型の引数1つ、R型の結果を返す

Sample.java
Function<String, Integer> lengthFunction = s -> s.length();
System.out.println(lengthFunction.apply("Hello"));  
// 出力: 5

・BiFunction<T,U,R>

T型とU型の引数2つ、R型の結果を返す

Sample.java
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
System.out.println(add.apply(3, 4));  
// 出力: 7

・Predicate<T>

T型の引数1つ、boolean型の結果を返す

Sample.java
Predicate<Integer> isPositive = x -> x > 0;
System.out.println(isPositive.test(5));  
// 出力: true

・BiPredicate<T,U>

T型とU型の引数2つ、boolean型の結果を返す

Sample.java
BiPredicate<String, String> equals = (s1, s2) -> s1.equals(s2);
System.out.println(equals.test("hello", "hello"));  
// 出力: true

・UnaryOperator<T>

T型の引数1つ、同じT型の結果を返す

Sample.java
UnaryOperator<Integer> increment = x -> x + 1;
System.out.println(increment.apply(5));  
// 出力: 6

・BinaryOperator<T>

T型の引数2つ、同じT型の結果を返す

Sample.java
BinaryOperator<Integer> max = (a, b) -> a > b ? a : b;
System.out.println(max.apply(3, 5));  
// 出力: 5

・Runnable

引数無し、結果を返さない

Sample.java
Runnable runnable = () -> System.out.println("タスク実行中...");
runnable.run();  
// 出力: タスク実行中...

・Callable<V>

引数無し、V型の結果を返す(例外をスローできる)

Sample.java
Callable<String> callable = () -> "Task result";
try {
    System.out.println(callable.call());
    // 出力: Task result
} catch (Exception e) {
    e.printStackTrace();
}

5.最後に

代表的な関数型インターフェースについて、
用途とそれぞれを利用したラムダ式の使用例を記載しました。
種類が多く、ラムダ式に利用する際の用途も、
引数の数や戻り値の有無や型の指定等、細かい差が多いため、
使用例を確認することで個人的には理解や覚えるのに役立ったと感じます。
自分の自己学習のアウトプットとして記載した記事ではありますが、
この記事を見たJava Goldの勉強をしている人の力になれば幸いです。

6.参考

71
3
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
71
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?