Java Reference ยท Streams and Functional
Primitive functional interfaces
Reference for the primitive specializations in java.util.function: IntFunction, ToIntFunction, IntPredicate, IntUnaryOperator, IntSupplier and their long and double kin.
Generic interfaces such as Function<Integer, Integer> work only with objects, so every int is boxed into an Integer and unboxed again. For hot loops that cost matters. The package java.util.function therefore has primitive specializations for int, long and double (and boolean for suppliers). They are also what the primitive streams IntStream, LongStream and DoubleStream accept.
The naming is regular. A prefix says which primitive is involved: Int, Long, Double. A To... prefix means the interface returns a primitive (ToIntFunction<T>: object in, int out). Obj... means an object plus a primitive as arguments. The method names include the primitive type for results: applyAsInt, getAsDouble, and so on.
IntUnaryOperator square = n -> n * n; int r = square.applyAsInt(7); // 49, no boxing
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
The main families (replace Int with Long or Double for the other two):
| Interface | Method | Meaning |
|---|---|---|
IntFunction<R> |
R apply(int) |
int to object |
ToIntFunction<T> |
int applyAsInt(T) |
object to int |
ToIntBiFunction<T,U> |
int applyAsInt(T,U) |
two objects to int |
IntUnaryOperator |
int applyAsInt(int) |
int to int |
IntBinaryOperator |
int applyAsInt(int,int) |
two ints to int |
IntPredicate |
boolean test(int) |
int to boolean |
IntConsumer |
void accept(int) |
consumes an int |
ObjIntConsumer<T> |
void accept(T,int) |
object and int consumed |
IntSupplier |
int getAsInt() |
produces an int |
IntToLongFunction, IntToDoubleFunction |
applyAsLong, applyAsDouble |
convert between primitives |
BooleanSupplier |
boolean getAsBoolean() |
produces a boolean |
There are no primitive interfaces for byte, short, char or float: use the int or double version. The Long and Double families mirror these shapes, including ObjLongConsumer and ObjDoubleConsumer.
Usage notes
- Avoid boxing in loops.
IntStream.map(IntUnaryOperator)creates no objects, whereasStream<Integer>.map(Function)does for every element. - Method names vary. Calling
apply()on anIntUnaryOperatordoes not compile; useapplyAsInt(). This is the most common mistake. - Composition is limited.
IntPredicatehasand,or,negate;IntUnaryOperatorhasandThen,compose; the others have none. - Choosing between
IntandToInt:ToIntFunction<T>starts from an object (use withmapToInt),IntFunction<R>starts from an int (use withmapToObj). - Overflow still applies.
IntBinaryOperatoradding two large ints wraps silently; useMath.addExactorlong. - No
charversions.String.chars()gives anIntStreamof code units; cast back tocharwhen printing.