Java Reference
In-Depth Information
List<Integer> list = new ArrayList<>();
for (int i = 300; i < 400; i++){
list.add(i);
}
But this comes with a performance cost. Boxed values are essentially a wrapper around
primitive types and are stored on the heap. Therefore, boxed values use more memory and
require additional memory lookups to fetch the wrapped primitive value.
Java 8 brings a specialized version of the functional interfaces we described earlier in order to
avoid autoboxing operations when the inputs or outputs are primitives. For example, in the
following code, using an IntPredicate avoids a boxing operation of the value 1000, whereas
using a Predicate<Integer> would box the argument 1000 to an Integer object:
In general, the names of functional interfaces that have a specialization for the input type
parameter are preceded by the appropriate primitive type, for example, DoublePredicate,
IntConsumer, LongBinaryOperator, IntFunction, and so on. The Function interface has also
variants for the output type parameter: ToIntFunction<T>, IntTo-DoubleFunction, and so on.
Table 3.2 gives a summary of the most commonly used functional interfaces available in the
Java API and their function descriptors. Keep in mind that they're only a starter kit. You can
always make your own if needed! Remember, the notation (T, U) -> R shows how to think about
a function descriptor. The left side of the table is a list representing the types of the arguments.
In this case it represents a function with two arguments of respectively generic type T and U and
that has a return type of R.
You've now seen a lot of functional interfaces that can be used to describe the signature of
various lambda expressions. To check your understanding so far, have a go at Quiz 3.4 .
Quiz 3.4: Functional interfaces
Search WWH ::




Custom Search