Java : Function with Examples

Function (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Function<T,R> methods.


Summary

Represents a function that accepts one argument and produces a result.

Class diagram

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]
// An example with a lambda expression.
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]

Methods

default <V> Function<T,V> andThen (Function<? super R,? extends V> after)

Returns a composed function that first applies this function to its input, and then applies the after function to the result.

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());
}

// Result
// ↓
//-- apply null --
//  func1 apply!
//IllegalArgumentException! : s is null

R apply (T t)

Applies this function to the given argument.

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]
// An example with a lambda expression.
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)

Returns a composed function that first applies the before function to its input, and then applies this function to the result.

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());
}

// Result
// ↓
//-- apply null --
//  before apply!
//IllegalArgumentException! : s is null

static <T> Function<T,T> identity ()

Returns a function that always returns its input argument.

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

Related posts

To top of page