Skip to content
Tutorials Center
My learning

Topics

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.

Java
IntUnaryOperator square = n -> n * n;
int r = square.applyAsInt(7);   // 49, no boxing

Try it

Java Primitive operators and predicates

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.

Java Conversions between types

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, whereas Stream<Integer>.map(Function) does for every element.
  • Method names vary. Calling apply() on an IntUnaryOperator does not compile; use applyAsInt(). This is the most common mistake.
  • Composition is limited. IntPredicate has and, or, negate; IntUnaryOperator has andThen, compose; the others have none.
  • Choosing between Int and ToInt: ToIntFunction<T> starts from an object (use with mapToInt), IntFunction<R> starts from an int (use with mapToObj).
  • Overflow still applies. IntBinaryOperator adding two large ints wraps silently; use Math.addExact or long.
  • No char versions. String.chars() gives an IntStream of code units; cast back to char when printing.