IntFunction Interface in Java with Examples


In Java, the IntFunction interface is a functional interface that represents a function that accepts an integer type value as an argument and returns a result of any data type. Here, the functional interface means an interface that contains only a single abstract method and exhibits single functionality. Some examples of functional interfaces are Predicate, Runnable, and Comparable interfaces. The IntFunction interface is defined in the 'java.util.function' package. In this article, we are going to explore the IntFunction Interface and its built-in methods with the help of example programs.

IntFunction Interface in Java

The IntFunction Interface has a single abstract method known as 'apply()'. But, before discussing this method let's see the syntax of IntFunction Interface.

Syntax

public interface IntFunction

It is necessary to import this Interface before using it in our program. To import the IntFunction Interface use the following command:

import java.util.function.IntFunction;

Use of apply() method of IntFunction Interface

The 'apply()' method is a built-in functional method of IntFunction Interface that accepts a single input of integer type and applies the IntFunction Interface to the given argument.

Syntax

instance.apply(int val)

Here, the 'instance' specifies the instance of IntFunction and 'val' specify the operand on which the operation will be performed.

Example 1

The following example demonstrates the use of 'apply()' method in printing the square and cube of a specified integer variable.

Approach

  • First, import the required package that was mentioned earlier.

  • Then, create two instances of IntFunction. The first one will return the square and the second one will return the cube of specified integer variable.

  • In the end, use the 'apply()' method along with the instance of IntFunction and pass the required integer value to perform both operations.

import java.util.function.IntFunction;
public class IntFunctionExample1 {
   public static void main(String[] args) {
      // creating instances of IntFunction
      IntFunction printSquare = n1 -> n1 * n1;
      IntFunction printCube = n2 -> n2 * n2 * n2;
      // to print the result
      System.out.println("Square of specified value: " + printSquare.apply(5));
      System.out.println("Cube of specified value: " + printCube.apply(2));
   }
}

Output

Square of specified value: 25
Cube of specified value: 8

Using streams with IntFunction Interface

In Java, the streams allow us to perform the functional operations on the specified element. It simply channelizes the elements of source such as classes of collection framework, through various built-in methods to return the result.

Example 2

In the following example, we will use the streams with the IntFunction Interface to convert the collection of integers into String.

Approach

  • First, import the required package so that we can work with streams and IntFunction Interface.

  • Create a list of integers.

  • Then, define an instance of IntFunction Interface that will perform the conversion of a integer into a string.

  • Now, use the instance of IntFunction Interface along with stream() and collect() to convert the integer to string. Here, stream() specifies input in the form of a stream.

import java.util.*;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
public class IntFunctionExample2 {
   public static void main(String[] args) {
      // creating a list of integers
      List<Integer> AList = Arrays.asList(11, 22, 33, 44, 55);
      // using IntFunction to convert elements to a String
      IntFunction<String> intToString = i -> String.valueOf(i);
      // Using the IntFunction to convert the list of integers into a list of strings
      List<String> strings = AList.stream()
         // convert Integer to int
            .mapToInt(i -> i) 
         // applying the IntFunction  
            .mapToObj(intToString) 
         // collecting the result
            .collect(Collectors.toList()); 
      // Printing the result
      System.out.println("The elements of the list: " + strings);
   }
}

Output

The elements of the list: [11, 22, 33, 44, 55]

Conclusion

In this article, we have learned about IntFunction Interface which is a type of functional interface. One of the advantages of using the IntFunction interface is that it allows us to write code using lambda expressions. Furthermore, we can use them with the Stream API or other methods that accept functional interfaces as parameters.

Updated on: 20-Jul-2023

216 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements