The shapes interviewers name
| Aspect | Interface | Shape and typical use |
|---|---|---|
| Function<T,R> | T in, R out | map: transform |
| Predicate<T> | T in, boolean out | filter |
| Consumer<T> | T in, void | forEach side effects |
| Supplier<T> | nothing in, T out | lazy creation |
| UnaryOperator<T> | T in, T out | replaceAll |
| BinaryOperator<T> | two T in, T out | reduce |
| BiFunction<T,U,R> | two inputs, one output | merge / compute |
Function<T,R>
InterfaceT in, R outShape and typical usemap: transformPredicate<T>
InterfaceT in, boolean outShape and typical usefilterConsumer<T>
InterfaceT in, voidShape and typical useforEach side effectsSupplier<T>
Interfacenothing in, T outShape and typical uselazy creationUnaryOperator<T>
InterfaceT in, T outShape and typical usereplaceAllBinaryOperator<T>
Interfacetwo T in, T outShape and typical usereduceBiFunction<T,U,R>
Interfacetwo inputs, one outputShape and typical usemerge / compute
Prefer IntPredicate, ToIntFunction, etc. on hot paths to avoid boxing.
public class Example {
public static void main(String[] args) {
Predicate<String> notBlank = text -> !text.isBlank();
Function<String, Integer> length = String::length;
Supplier<List<String>> newList = ArrayList::new;
Predicate<String> valid = notBlank.and(text -> text.length() <= 20);
Function<String, Integer> stripThenLength = ((Function<String, String>) String::strip).andThen(String::length);
}
}
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 2 sections
- Your own functional interface
- Predicate helpers (Java 11+)
Test yourself
Answer these before moving on — recall is what makes it stick.