Functional Interface Specializations in Java - Quiz

Total: 21 questions

1. 

Enumerate the most commonly used functional interfaces.

Predicate, Consumer, Supplier, Function and UnaryOperator.

2. 

The purpose of binary specializations of Predicate, Consumer, Function and UnaryOperator common interfaces.

The Predicate, Consumer, Function and UnaryOperator functional interfaces represent an operation that takes one argument. Their binary versions take two arguments called. They have the same semantics, the only difference is the number of arguments.

3. 

The binary version of the Consumer interface.

BiConsumer<T, U>

4. 

The binary version of the Function interface.

BiFunction<T, U, R>

5. 

The binary version of the Predicate interface.

BiPredicate<T, U>

6. 

The binary version of the UnaryOperator interface.

BinaryOperator<T>

7. 

The purpose of primitive specializations of Predicate, Consumer, Function, Supplier and UnaryOperator common interfaces.

Primitive specializations give the possibility to avoid autoboxing operations when the inputs or outputs are primitives.

8. 

Rewrite the example using primitive specialization:

Consumer<Integer> ic = i -> System.out.println(++i);
ic.accept(8);
ic.accept(9);
IntConsumer ic = i -> System.out.println(++i);
ic.accept(8);
ic.accept(9);
9. 

Enumerate primitive specializations of Consumer functional interface.

IntConsumer

LongConsumer

DoubleConsumer

10. 

Enumerate consuming primitive specializations of the Function functional interface.

IntFunction<R>

LongFunction<R>

DoubleFunction<R>

 

11. 

Enumerate producing primitive specializations of the Function functional interface.

ToIntFunction<T>

ToLongFunction<T>

ToDoubleFunction<T>

12. 

Enumerate producing/consuming primitive specializations of the Function functional interface.

IntToDoubleFunction

IntToLongFunction

LongToDoubleFunction

LongToIntFunction

DoubleToIntFunction

DoubleToLongFunction

13. 

Enumerate primitive specializations of Supplier functional interface.

BooleanSupplier

IntSupplier

LongSupplier

DoubleSupplier

 

14. 

Enumerate primitive specializations of UnaryOperator functional interface.

IntUnaryOperator

LongUnaryOperator

DoubleUnaryOperator

 

 

15. 

Enumerate primitive specializations of BinaryOperator functional interface.

IntBinaryOperator

LongBinaryOperator

DoubleBinaryOperator

 

16. 

Enumerate primitive specializations of BiConsumer functional interface.

ObjIntConsumer<T>

ObjLongConsumer<T>

ObjDoubleConsumer<T>

17. 

 Enumerate primitive specializations of BiFunction functional interface.

ToIntBiFunction<T, U>

ToLongBiFunction<T, U>

ToDoubleBiFunction<T, U>

 

18. 

Enumerate primitive specializations of the Predicate functional interface. Does BiPredicate have a primitive version?

IntPredicateboolean test(int)

LongPredicateboolean test(long)

DoublePredicateboolean test(double)

BiPredicate has no primitive specializations: there is no IntBiPredicate or ObjIntPredicate in the package.

19. 

How do you reconstruct the signature of a java.util.function interface from its name?

The names follow a strict pattern:

Int / Long / Double prefix — the input type: IntPredicateboolean test(int).

To + type — the return type: ToIntFunction<T>int applyAsInt(T).

type + To + type — both input and output are primitive: IntToDoubleFunctiondouble applyAsDouble(int).

Obj + type — an object plus a primitive: ObjIntConsumer<T>void accept(T, int).

Bi prefix — two object arguments: BiFunction<T, U, R>R apply(T, U).

UnaryOperator / BinaryOperator — all types are the same: IntBinaryOperatorint applyAsInt(int, int).

When the result is primitive, the method name gets the As + type suffix: applyAsInt, getAsDouble. The test() and accept() methods have no suffix — their return types are already fixed.

20. 

Which functional interfaces do the Stream and IntStream methods expect: mapToInt(), map(), mapToObj(), filter(), forEach(), reduce()?

Stream<T>.mapToInt()ToIntFunction<T>, switches to an IntStream.

IntStream.map()IntUnaryOperator.

IntStream.mapToObj()IntFunction<R>, switches back to a Stream<R>.

IntStream.filter()IntPredicate.

IntStream.forEach()IntConsumer.

IntStream.reduce()IntBinaryOperator.

Also: Map.forEach() expects a BiConsumer<K, V>, Map.merge() a BiFunction<V, V, V>, and List.replaceAll() a UnaryOperator<E>.

21. 

Which primitive types have specializations, and do those specializations inherit the generic interfaces?

Specializations exist only for int, long and double (plus BooleanSupplier for boolean). There are no interfaces for float, byte, short or char — the int and double versions are used instead, relying on widening conversion.

Primitive specializations do not inherit the generic interfaces: IntPredicate is not a subtype of Predicate<Integer>, and IntBinaryOperator is not a subtype of BiFunction. They are unrelated types and cannot be assigned to each other.

Page 1 of 1