java.util.function
package.
andThen()
and compose()
methods for multiple transformations.public interface Function
{
R apply(T t);
//some default methods are also present
}
import java.util.function.Function;
public class MainApp
{
public static void main(String[] args)
{
// Function to find square of a number using anonymous class
Function<Integer, Integer> square = new Function<Integer, Integer>() {
@Override
public Integer apply(Integer x) {
return x * x;
}
};
// Test numbers
System.out.println("Square of 5 : " + square.apply(5));
System.out.println("Square of 9 : " + square.apply(9));
}
}
Square of 5 : 25 Square of 9 : 81
import java.util.function.Function;
public class MainApp
{
public static void main(String[] args)
{
// Function to find square of a number using lambda expression
Function<Integer, Integer> square = x -> x * x;
// Test numbers
System.out.println("Square of 5 : " + square.apply(5));
System.out.println("Square of 9 : " + square.apply(9));
}
}
Square of 5 : 25 Square of 9 : 81
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.