Skip to content

Supplier Functional Interface & Lazy

imrajsinghsandhu edited this page Nov 24, 2021 · 1 revision

What is the Supplier Functional Interface? What is Lazy?

We've seen this being used so many times, but I did not have much of an idea of what it really meant, and why we were using it. After doing some research, I present to everyone what I have found out!

Supplier

The Supplier interface represents an operation that takes no argument and returns a result. For example, Supplier<T> s = () -> foo(); . You would pass s to a new Constructor(Supplier<T> supplier);, which then updates the supplier attribute of that newly instantiated class. Remember, Supplier is a functional interface meaning you'd have to pass it as a function.

Lazy

The idea of Lazy evaluation comprises of two things: 1. Delayed Evaluation, 2. Memoization(Caching) That's pretty much all you need to know about the brief overview of Lazy! In other words, lazy eval is all about optimization --> you don't evaluate what you don't need, and once evaluated it is memoized.

Imagine you're a Software Engineer at ABC company, and you have a huge list of say 10 million elements. Assuming that you manipulate your list multiple times or are evaluating certain sequences, it would be extremely inefficient to not have this list in a Lazy format. This would drastically worsen the run time of your code, which can be improved in certain situations by making the list Lazy.

I strongly suggest catching up on Prof Henry's lecture 9. I was very confused before watching the lecture, but just 30 mins into it I was able to clear most of my misunderstandings on Lazy and the Supplier functional Interface.

Clone this wiki locally