Lambda: ToIntFunction and IntFunction convert on opposite sides of a boundary

ToIntFunction<T> takes an object and returns a primitive int, while IntFunction<R> takes a primitive int and returns an object; together they cover both directions of crossing the boxing boundary.

Code
ToIntFunction<String> length = String::length;
IntFunction<String> repeatX = n -> "x".repeat(n);
System.out.println("length.applyAsInt(\"hello\") = " + length.applyAsInt("hello"));
System.out.println("repeatX.apply(3) = " + repeatX.apply(3));
Output
length.applyAsInt("hello") = 5
repeatX.apply(3) = xxx
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms