Java functions and functional interfaces

I have enough knowledge of Lambdas In Java to be able to use them where useful, but under the covers there’s more to how functions are defined and referenced using the interfaces in java.util.function. Here’s a few notes digging into more details on implementing functions in Java.

java.util.function interfaces

Java interfaces used to define types for Lambda expressions and method references:

interface Function<T, R>
Defines a function which takes a parameter of type T and returns a result of type R

Single argument functions:

interface Consumer<T>
A function that takes a parameter of type T but with no return value. Provides single function void accept(T)

interface Supplier<T>
A function that takes no argument but returns a result of type T. Provides single functional method T get()

To define a function that doesn’t take any parameters or return a result can be represented by interface Java.lang.Runnable

All other interfaces in java.util,function are variations of Provider and Supplier, such as …

Bi Function interfaces

BiFunction<T, U, R>
A function that takes 2 parameters of type T and U and returns result of type R

BiConsumer<T, U>
A function that takes 2 parameters of types T and U but no return type.

Other interfaces

Boolean Predicate<T>
A function that takes a parameter of type T and returns a Boolean result. Example usage is for a filter function on Streams

BiPredicate<T, U>

<T> UnaryOperator<T>
Special case of Function that takes an argument and returns the result of the same type

<T> BinaryOperator<T>

Takes 2 parameters of the type and results a result of the same type

Method references

Method references use the :: syntax to refer to a static method that exists on a Class and pass this as a reference in place of a Lambda. For example System.out::println

To print each element of a list you could pass a Lambda calling println for each item:

list.forEach( item -> System.out.println(item)):

but using a method reference simplifies this to:

list.forEach(System.out::println);

Method references implement a functional interface but instead of implementing a new method like with a Lambda, a Method reference points to a method that already exists in a Class.

Mwthod references can be used to a static method with

Typename::staticMethodName

or to a method on an instance with

instanceName::instanceMethodName

Quick tip of the day: While learning Java, avoid defining anything as static

If you’re just starting out and learning Java, avoid the temptation of defining anything as static until you understand what this modifier does and why/when you need to use it: no static Class properties, no static methods.

The only use of static while you’re starting out is in your main method. You don’t need it anywhere else. You’ll avoid a lot of unexpected and unexplainable behavior as a result.

Effective Java: recommended 18 years ago, still top of my recommended reading list today

In past years I used to put together a recommended reading list for software developers, particularly Java developers. I think I stopped doing it as often because year to year my list really didn’t change much, if at all.

As an example, 18 years ago, Effective Java by Josh Bloch I would say was required reading for all Java developers, new and experienced. It would still be at the top of my list today. It’s essential reading and solid guidance for all Java developers. If you’re a Java developer and you haven’t read this book yet, pick up a copy and read it now.