The LongToIntFunction and ToIntFunction are functional interfaces provided in the java.util.function package. Both accept a single input and return an int result, but they differ in the type of input they accept.
- LongToIntFunction works specifically with a primitive long input.
- ToIntFunction<T> works with an input of type T.
- Both interfaces use the applyAsInt() method to perform the operation and return an int value.
Example: Program to demonstrates the use of LongToIntFunction and ToIntFunction<T> to accept different types of input and return an int result.
import java.util.function.LongToIntFunction;
import java.util.function.ToIntFunction;
public class GFG {
public static void main(String[] args) {
LongToIntFunction longFunction =
value -> (int) value * 100;
ToIntFunction<Double> intFunction =
value -> (int) (value * 10);
System.out.println(longFunction.applyAsInt(20L));
System.out.println(intFunction.applyAsInt(3.2));
}
}
Output
2000 32
Explanation: The LongToIntFunction accepts a primitive long value 20L and multiplies it by 100, returning 2000 as an int. The ToIntFunction<Double> accepts a Double value 3.2, multiplies it by 10, and returns 32 as an int.
Interfaces for Functions Returning int
Java provides LongToIntFunction and ToIntFunction<T> for representing functions that produce an int result from a single input. The main difference between them is the type of input passed to the function.
LongToIntFunction
LongToIntFunction is a functional interface that accepts a primitive long value and returns an int result. It is useful for operations where the input specifically needs to be a long.
Syntax
@FunctionalInterface
public interface LongToIntFunction {
int applyAsInt(long value);
}
Example: The following program uses a lambda expression with LongToIntFunction to perform an operation on a long value.
import java.util.function.LongToIntFunction;
public class GFG {
public static void main(String[] args) {
LongToIntFunction operation =
value -> (int) value * 100;
System.out.println(operation.applyAsInt(20L));
}
}
Output
2000
Explanation: The lambda expression receives 20L as a long value, multiplies it by 100, and returns the result as an int.
Working of LongToIntFunction
This flow demonstrates how LongToIntFunction accepts a primitive long value and produces an int result.
- A primitive
longvalue is passed to theapplyAsInt()method. - The
applyAsInt()method passes the value to the lambda expression. - The lambda expression performs the specified operation on the
longvalue. - The operation returns the result as an
int.
ToIntFunction
ToIntFunction<T> is a functional interface that accepts an argument of type T and returns an int result. The generic type T allows the interface to work with different input types.
Syntax
@FunctionalInterface
public interface ToIntFunction<T> {
int applyAsInt(T value);
}
Example: The following program uses ToIntFunction<Double> to accept a Double value and produce an int result.
import java.util.function.ToIntFunction;
public class GFG {
public static void main(String[] args) {
ToIntFunction<Double> operation =
value -> (int) (value * 10);
System.out.println(operation.applyAsInt(3.2));
}
}
Output
32
Explanation: Here, T is Double, so the function accepts a Double value. The lambda expression multiplies the value by 10 and returns the result as an int.
Working of ToIntFunction<T>
This flow demonstrates how ToIntFunction<T> accepts a value of type T and produces an int result.
- A value of type T is passed to the applyAsInt() method.
- The applyAsInt() method passes the value to the lambda expression.
- The lambda expression performs the specified operation on the input.
- The operation returns the result as an int.
Method of LongToIntFunction and ToIntFunction
Both LongToIntFunction and ToIntFunction<T> provide the applyAsInt() method to apply the defined operation and return an int result.
applyAsInt()
For LongToIntFunction, the method accepts a primitive long, while ToIntFunction<T> accepts a value of the generic type T.
Syntax
int applyAsInt(long value);
int applyAsInt(T value);
Example: This program demonstrates how applyAsInt() applies lambda expressions to different input types and returns int results.
import java.util.function.LongToIntFunction;
import java.util.function.ToIntFunction;
public class GFG {
public static void main(String[] args) {
LongToIntFunction longFunction =
value -> (int) value * 100;
ToIntFunction<Double> intFunction =
value -> (int) (value * 10);
System.out.println(longFunction.applyAsInt(20L));
System.out.println(intFunction.applyAsInt(3.2));
}
}
Output
2000 32
Explanation: The first applyAsInt() call passes 20L to the LongToIntFunction, while the second passes 3.2 to the ToIntFunction<Double>. Both lambda expressions perform their operations and return the results as int values.
Working of applyAsInt()
This flow demonstrates how applyAsInt() passes the input to the defined lambda expression and returns the resulting int value.
- An input value is passed to applyAsInt().
- applyAsInt() passes the value to the lambda expression.
- The lambda expression performs the specified operation.
- The operation returns the result as an int.
Difference Between LongToIntFunction and ToIntFunction<T>
| Feature | LongToIntFunction | ToIntFunction<T> |
|---|---|---|
| Input Type | Primitive long | Generic type T |
| Return Type | Primitive int | Primitive int |
| Method | applyAsInt(long value) | applyAsInt(T value) |
| Generic Type | No generic type | Uses generic type T |
| Use Case | When the input is specifically a long | When the input is of type T |
| Primitive Specialization | long → int | T → int |