0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

関数型インタフェース紹介

Posted at

みなさん、こんにちは。
今回関数型のインタフェースを紹介させていただきます。

Predicate (戻り値→ boolean)

public interface Predicate <T>

メソッド例:

import java.util.function.Predicate;

public class App {
    public static void main( String[] args ) {

        Predicate <String> pre = (String username) -> {
            return "admin".equals(username);
        };

        System.out.println(pre.test("manager"));
        System.out.println(pre.test("admin"));
      }
    }

戻り値:

false
true

Consumer (戻り値なし)

public interface Consumer<T>
import java.util.function.Consumer;

public class App {
    public static void main( String[] args ) {
        Consumer<String> con = (String message) -> {
            System.out.println("Return message: " + message);
            System.out.println("The message send succeed");
        };
        con.accept("Hello world");
        con.accept("Lambda");
      }
    }

Function

public interface Function<T,R>

例:

import java.util.function.Function;

public class App {
    public static void main( String[] args ) {

        Function<String, Integer> fun = (String gender) -> {
            return ("male".equals(gender))?0:1;
        };
        System.out.println(fun.apply("male"));
        System.out.println(fun.apply("female"));
      }
    }

結果:

0
1
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?