Why I love java.util.function?

Govinda Raj
The Startup
Published in
5 min readSep 10, 2020

One of the greatest things that happened to Java8 is FunctionalInterface . It’s amazing. It makes java developer’s life so functional that you will love it. By name, we get some idea that it is an interface and it’s functional too. Ohh…
As far as I know, an interface can not have implementation and a function is nothing but implementation only.

Should we say that java creators have skimped while finding an appropriate name to this feature???

Source

I would say, they were not skimping instead they were very clever to give it a name as FunctionalInterface . As we know, the interface provides you a way to implement your own version of the prototype. So they decided that they will give some prototype(here a predefined function(not a method)) to users and let them implement their own version and use it as they want… hurrrrrrah. But it might not have been possible if there were no generic concepts.

Ohh God, they are very clever, they know how to use the existing features massively.
So here comes all the masterpieces of java-8-functional-interfaces. I call them base functional interfaces.

  1. Function
  2. Predicate
  3. Supplier
  4. Consumer

We will not be seeing any code here, but we will just try to understand it by imagining it.

So what is Function ?

I am asking about the real-life bro? What is a function in your real life?

A function is some sort of event where some people come and do bla bla and then go back. > yeah???

This Function is also very similar to real-life functions, which takes some input and do some bla bla then returns back. So now let’s give it a proper(in normal language) definition: — It takes one type of input and returns one type of output. Here, the type can be any concrete substance(Object).

Now let’s understand Consumerfirst: — We can think of it as a real-life consumer who takes one type of input and do some process but does not return anything. Let’s understand this: — I want someone to do some tasks, but I don’t want to know whether that person completed that task or not. I will just ask him to do it that’s it.

Supplier : does not take any input but returns one type of output. You can think of a supplier as — bro I want this, you bring it from anywhere I don’t care.

Predicate : as the word itself says: — something which is affirmed or denied concerning an argument of a proposition. Basically a Predicate functional interface gives you a prototype to assert something you pass it to.

Now, I hope you understand the literal meaning of all above mentioned functional interfaces. If not, please let me know in the comment section below!

There are much more functional interfaces, in which some are enhanced form of base functional interfaces or some are in customized forms. So Let’s look into enhanced form first: —

  1. BiFunction: Takes two types of input and perform some task then returns one type of output.
  2. BiConsumer: Takes two types of input and perform some task.
  3. BiPredicate: Assert on two types of input.
  4. BinaryOperator: Takes two same type of input and returns the same type of output as the input’s type.
  5. UnaryOperator: Takes one type of input and returns the same type of output as the input’s type.

Now Let’s focus on all customized forms: —

  1. IntFunction: Takes Integers as input and performs some operations and returns any type of output.
  2. ToIntFunction: Takes any type of input and performs some operations and returns Integer.
  3. LongFunction: Takes Long data type as input and performs some operations and returns any type of output.
  4. ToLongFunction: Takes any type of input and performs some operations and returns a long data type.
  5. DoubleFunction: Takes Double data type as input and performs some operations and returns any type of output.
  6. ToDoubleFunction: Takes any type of input and performs some operations and returns a double data type.
  7. DoubleToIntFunction: Takes Double data type of input and performs some operations and returns an Integer data type.
  8. DoubleToLongFunction: Takes Double data type of input and performs some operations and returns a Long data type.
  9. IntToLongFunction: Takes Integer data type of input and performs some operations and returns a Long data type.
  10. IntToDoubleFunction: Takes Integer data type of input and performs some operations and returns a Double data type.
  11. LongToIntFunction: Takes a Long data type of input and performs some operations and returns an Integer data type.
  12. LongToDoubleFunction: Takes a Long data type of input and performs some operations and returns a Double data type.
  13. IntConsumer: Just takes an integer and performs some operations.
  14. LongConsumer: Just takes a long data type value and performs some operations.
  15. DoubleConsumer: Just takes a double data type value and performs some operations.
  16. BooleanSupplier: Just returns a boolean value.
  17. IntSupplier: Just returns an integer value.
  18. LongSupplier: Just returns a long value.
  19. DoubleSupplier: Just returns a double value.
  20. IntPredicate: Assert on an integer value.
  21. LongPredicate: Assert on a long value.
  22. DoublePredicate: Assert on a double value.

There are some enhanced customized forms also: —

  1. ToIntBiFunction: Takes two types of input and performs some operations then returns an Integer data type.
  2. ToLongBiFunction: Takes two types of input and performs some operations then returns a Long data type.
  3. ToDoubleBiFunction: Takes two types of input and performs some operations then returns a Double data type.
  4. IntUnaryOperator: Takes an integer and perform some operations and returns an integer.
  5. LongUnaryOperator: Takes a long and perform some operations and returns a long date type.
  6. DoubleUnaryOperator: Takes a double and perform some operations and returns a double.
  7. IntBinaryOperator: Takes two integers and performs some operations and returns an integer.
  8. LongBinaryOperator: Takes two long data types and performs some operations and returns a long data type.
  9. DoubleBinaryOperator: Takes two doubles and performs some operations and returns a double.
  10. ObjIntConsumer: Special type of consumer which takes two inputs in which the second input is always an integer.
  11. ObjLongConsumer: Special type of consumer which takes two inputs in which the second input is always a long value.
  12. ObjDoubleConsumer: Special type of consumer which takes two inputs in which the second input is always a double value.

Hey man! Can you help me to write a function to add two integers???

Hey bro! Can you help me to write a function to find if a String starts with ‘D’???

Hey buddy! I will give you data to put into the cache. Can you do it for me?

Hey, Can you return me a failure message?

They(Java creators) have given us many functions to implement and use it. Isn’t it beautiful? Once you start using these functions your code readability will improve so much.

That’s all for this blog. Let me know if you found this interesting.

You can reach me on twitter.

Thanks.

--

--

Govinda Raj
The Startup

Senior Software Developer. Tech Enthusiast and love coding. My portfolio: https://govinda-raj.github.io/