Lambdas and Streams

Rule 3:Prefer using standard Functional Interface

Why? it ,akes your API easier to learn by REDUCING the CONCEPTUAL SURFACE AREA
Functional Interface can be used for assignment of an lambda expression .
there are total 43 inbuild functional interfaces in package: java.util.function,you are not expected to remember all 43 but there are generic 6 based on input arguments and what they return – you can correlate with them.
Following are the 6 Functional Interfaces:

Predicate -takes ONE argument and returns the boolena
Function- arguments and the return type are different
BinaryOperator -argumemnts and the return type are same.
UnaryOperator-arguments and the return type are same
Consumer- it accepts One argument but doesnot return anything i.e. consumes on argument
Supplier-it takes no argument but has a return value.

There are 3 functional interfaces corresponding to each of above generic functional interface to represent the primary data type argument and return type.
Naming for inbuilt functional Interfaces-
Function has 9 additional varients:
if the result type is of primitive then it’s named like SrcToResultFucntion(6 varients) e.g. LongToIntFunction
if the result type is Object Reference it is named and ToObj e.g. DoubleToObjFunction(three varients)

If you decide to go with your own functional interface then,
mark the interface with annotation @FunctionalInterface, so that it guarentees that it will not compile till u have 1 abstract methid and in nfuture it will not accept any further methods as being Functional Interface and to be used for Lambdas.

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment