Java : Function with Examples

Function (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Function methods.


Summary

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

Class diagram

final var stream = Stream.of("0x1", "0xa", "0xff");

final var func = new Function<String, Integer>() {
    @Override
    public Integer apply(String s) {
        return Integer.decode(s) * 10;
    }
};

final var ret = stream.map(func).toList();
System.out.println(ret); // [10, 100, 2550]
final var stream = Stream.of("0x1", "0xa", "0xff");

final var ret = stream.map(s -> Integer.decode(s) * 10).toList();
System.out.println(ret); // [10, 100, 2550]

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) {
        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) {
        return s.toUpperCase();
    }
};

try {
    final var func2 = func1.andThen(after);
    func2.apply(null);
} catch (IllegalArgumentException e) {
    System.out.println(e);
}

// Result
// ↓
//java.lang.IllegalArgumentException: s is null

R apply (T t)

Applies this function to the given argument.

final var func = new Function<String, Integer>() {
    @Override
    public Integer apply(String s) {
        return Integer.decode(s) * 10;
    }
};

final var ret = func.apply("0xff");
System.out.println(ret); // 2550
// An example with a lambda expression.
final var stream = Stream.of("0x1", "0xa", "0xff");

final var ret = stream.map(s -> Integer.decode(s) * 10).toList();
System.out.println(ret); // [10, 100, 2550]

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) {
        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) {
        return s.toUpperCase();
    }
};

try {
    final var func2 = func1.compose(before);
    func2.apply(null);
} catch (IllegalArgumentException e) {
    System.out.println(e);
}

// Result
// ↓
//java.lang.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