What is the IntToLongFunction functional interface in Java?

Overview

IntToLongFunction is a functional interface that accepts one argument of type int and returns a result of type long. The interface contains one method, applyAsLong.

The IntToLongFunction interface is defined in the java.util.function package. To import the IntToLongFunction interface, use the following import statement.

import java.util.function.IntToLongFunction;

The applyAsLong(int value) method

The applyAsLong(int value) method applies the function to the given argument of type int and produces a result of type long. This is the functional method of the interface.

Syntax

long applyAsLong(int value)

Parameters

  • int value: the function argument of type int.

Return value

The method returns the function result of type long.

Code

import java.util.function.IntToLongFunction;
public class Main{
public static void main(String[] args) {
// Implementation of IntToLongFunction interface that returns square of the int value
IntToLongFunction intToLongFunction = value -> (long) value * value;
int value = 34;
// calling applyAsLong method of IntToLongFunction
System.out.println(intToLongFunction.applyAsLong(value));
}
}

Explanation

In the code above, we create an implementation of the IntToLongFunction interface that accepts an int argument and returns the square of the argument as a long value.

Free Resources