0Pricing
Java Academy · Lesson

Function and BiFunction

Transform values functionally.

Function and BiFunction is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Function Represents

Function<T, R> is the core transformation interface in java.util.function. It takes one argument of type T and returns a result of type R.

  • Its single abstract method is R apply(T t).
  • It models a pure mapping from input to output.
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<String, Integer> length = s -> s.length();
        System.out.println(length.apply("hello"));
    }
}

Calling apply

You invoke a Function by calling apply. The lambda body is the transformation logic.

  • Input and output types can differ.
  • A method reference like String::toUpperCase can replace a lambda.
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<String, String> upper = String::toUpperCase;
        System.out.println(upper.apply("java"));
    }
}

andThen Composition

andThen chains functions: the result of the first becomes the input of the second.

  • f.andThen(g) means g(f(x)).
  • Reads left to right.
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<Integer, Integer> times2 = n -> n * 2;
        Function<Integer, Integer> plus3 = n -> n + 3;
        System.out.println(times2.andThen(plus3).apply(5));
    }
}

compose Composition

compose runs the argument function first.

  • f.compose(g) means f(g(x)).
  • It is the mirror image of andThen.
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<Integer, Integer> times2 = n -> n * 2;
        Function<Integer, Integer> plus3 = n -> n + 3;
        System.out.println(times2.compose(plus3).apply(5));
    }
}

Function.identity

Function.identity() returns a function that gives back its input unchanged.

  • Useful in stream collectors and default mappings.
  • Equivalent to x -> x.
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<String, String> id = Function.identity();
        System.out.println(id.apply("unchanged"));
    }
}

Meet BiFunction

BiFunction<T, U, R> takes two arguments and returns one result.

  • Its method is R apply(T t, U u).
  • Great for combining two values, like merging maps.
import java.util.function.BiFunction;

public class Main {
    public static void main(String[] args) {
        BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
        System.out.println(add.apply(4, 6));
    }
}

BiFunction with Mixed Types

The three type parameters of BiFunction can all differ.

  • Here we combine a String and an int into a String.
import java.util.function.BiFunction;

public class Main {
    public static void main(String[] args) {
        BiFunction<String, Integer, String> repeat = (s, n) -> s.repeat(n);
        System.out.println(repeat.apply("ab", 3));
    }
}

BiFunction andThen

BiFunction also supports andThen, taking a single-arg Function to post-process the result.

  • There is no compose on BiFunction because it would need two inputs.
import java.util.function.BiFunction;
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
        Function<Integer, String> label = n -> "sum=" + n;
        System.out.println(add.andThen(label).apply(7, 8));
    }
}

Functions in Collections

Many APIs accept a Function. For example Map.computeIfAbsent takes a mapping function for missing keys.

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> lengths = new HashMap<>();
        lengths.computeIfAbsent("hello", String::length);
        System.out.println(lengths);
    }
}

Storing Functions in Variables

Because functions are objects, you can store them, pass them, and return them. This is the foundation of functional-style Java.

import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<Integer, Integer> square = n -> n * n;
        Function<Integer, Integer> cube = n -> n * n * n;
        Function<Integer, Integer> chosen = square;
        System.out.println(chosen.apply(5));
    }
}

Building a Pipeline

Chaining andThen repeatedly builds a transformation pipeline that is readable and reusable.

import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<String, String> trim = String::trim;
        Function<String, String> upper = String::toUpperCase;
        Function<String, Integer> len = String::length;
        System.out.println(trim.andThen(upper).andThen(len).apply("  abc "));
    }
}

Quick Check

What does f.andThen(g).apply(x) compute?

Recap

You learned the core transformation interfaces:

  • Function<T,R> maps one value with apply.
  • andThen chains forward (g(f(x))); compose chains backward (f(g(x))).
  • Function.identity() returns its input.
  • BiFunction<T,U,R> handles two inputs and supports andThen but not compose.

Frequently asked questions

Is the “Function and BiFunction” lesson free?

Yes — the full text of “Function and BiFunction” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Function and BiFunction”?

Transform values functionally. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Function and BiFunction” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Function and BiFunction
  2. Supplier and Consumer
  3. Predicate and Composition
  4. Custom Functional Interfaces
← Back to Java Academy