Java 8 ExamplesJava8

IntFunction in Java 8 With Code Sample

Posted On
Posted By admin

In this blog post, I will be explaining how the Java 8 functional interface IntFunction works. To know more about functional interfaces, you can refer to this blog post.

What is IntFunction

IntFunction is an in-built functional interface in the java.util.Function package. It accepts an argument of int data type, operates on it and produces a result of any data type. It is a specialization of the Function interface. It has an apply method. It applies the logic in this method on the int argument passed in and produces a result of the specified data type.

IntFunction Code Sample

The following code demonstrates this interface:

IntFunction<Double> doubleRetriever = input -> Double.valueOf(input);
System.out.println("Double value " + doubleRetriever.apply(5));

This code implements this interface via a lambda expression that returns a result of Double type. It simply returns a Double value corresponding to the int input passed in.

This code prints the following output:

Double value 5.0

Why IntFunction

IntFunction is a primitive specialization of the Function interface. The other functional interfaces like Supplier, Predicate,Consumer also have primitive specializations like IntSupplier, LongPredicate, IntConsumer, etc. The primitive specializations help to improve performance when the input parameters are of primitive types. For example, in the above example, we can also use a Function interface as follows:

Function<Integer,Double> doubleRetriever2 = input -> Double.valueOf(input);
System.out.println("Double value " + doubleRetriever2.apply(5));

This code uses a Function interface. It then invokes the apply method on the value 5. Since the input value is a primitive type, Java uses autoboxing to convert the primitive int type to an Integer before applying the lambda expression. This reduces the performance slightly. However, in the case of IntFunction, no such conversion is required since the IntFunction.apply method accepts an argument of type int already. Thus the primitive specialization offers a slight performance advantage as it does away with the need of autoboxing.

Conclusion

So in this article, we took a look at the IntFunction interface. This interface is a specialization of the Function interface that accepts an int parameter and returns a result on any data type.

If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my new course Learn Java 8 New Features

If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment