Functional Interfaces in Java with Lamda and Method References

Nitin Kalra
2 min readJan 26, 2020

--

Photo by timJ on Unsplash

Let's learn about Functional Interfaces first, there are more than 50 functional interfaces in java.

So, what is a Functional Interface — It is just an informative annotation. 🤷‍♂️ 😐

An informative annotation type used to indicate that an interface
type declaration is intended to be a functional interface

Conceptually, a functional interface has exactly one abstract method, Since default methods have an implementation, they are not abstract.

If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere

Conceptually, a functional interface has exactly one abstract method

Some of the most used functional interfaces with lamda are:-

UnaryOperator

Represents an operation on a single operand that produces a result of the same type as its operand.

BinaryOperator

Represents an operation upon two operands of the same type, producing a result of the same type as the operands.

Predicate

Evaluates this predicate on the given argument

Function

Represents a function that accepts one argument and produces a result. Applies this function to the given argument

Consumer

Represents an operation that accepts a single input argument and returns no result. This is a terminal operation on streams.

Supplier

Represents a supplier of results. That takes nothing and returns something.

In each of the above examples, I have tried to use a Functional Interface with an Anonymous Inner class and the same example is converted to a Lambda Expression and then converted to a Method Reference

Method reference is a shorthand notation of a lambda expression to call a method. :: operator is used in method reference to separate the class or object from the method name.

Complete code is uploaded at: https://gist.github.com/nkalra0123/1715dd0fa18af8159d954874ba72cfa9

If you found this article useful, you know what to do now. Hit that clap button and star the GitHub repo follow me to get more articles and tutorials on your feed.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response