LongFunction interface in Java


A LongFunction in Java is a functional interface which takes one argument of type long and returns a result of some other type. Functional interface is an interface that have only one abstract method. To use this interface first you have to import java.util.function package. 

The functional method of this interface is ‘apply()’ which takes long value as an argument and returns function result denoted by R.

Syntax

R apply(long value);

Algorithm

In order to effectively implement this functional interface into your work, there are three defining steps one must abide by beforehand.

  • First off, establishing an instance for such an implementation necessitates defining a suitable lambda expression which accurately previews its upcoming usage within its "apply" function when provided with long type variables or arguments.

  • Once done setting up your specific algorithm via Step 1 here, proceed onto executing Step 2 - calling "apply" on said instance while adding in parameter inputs for long types respectively. Once completed satisfactorily executed Steps 1 & 2 accordingly comes our third and final step - using all output values from prior steps within available areas throughout our ongoing code.

Approach 1: Define a Lamda Expression

You can define a lambda expression to implement the apply(long value) method of the LongFunction interface.

Example

import java.util.function.LongFunction;
public class LongFunctionExample {
   public static void main(String[] args) {
      LongFunction<String> longToString = l -> Long.toString(l);
      String result = longToString.apply(123L);
      System.out.println(result);
   }
}

Output

123

Explanation

This code creates a LongFunction that converts a long value to a String

Approach 2: Using a Method Reference

You can use a method reference to implement the apply(long value) method of the LongFunction.

Example

import java.util.function.LongFunction;
public class LongFunctionExample {
   public static void main(String[] args) {
      LongFunction<Integer> countDigits = Long::toString().length;
      int result = countDigits.apply(12345L);
      System.out.println(result);
   }
}

Output

12345

Explanation

created a LongFunction that returns the number of digits in a long value.

Approach 3: Using Pre Defined Function

To use a predefined function that implements the LongFunction interface, you can create an instance of the interface and pass a predefined function as a lambda expression or method reference.

Example

import java.util.function.LongFunction;
public class LongFunctionExample {
   public static void main(String[] args) {
      LongFunction<Double> sqrt = Math::sqrt;
      double result = sqrt.apply(16L);
      System.out.println(result);
   }
}

Output

4.0

Explanation

In the above sqrt pre defined function that implements the LongFunction.

Comparison Between Approaches

Criteria

Approach 1

Approach 2

Approch 3

Type

Lamda expression

Method reference

Predefined function

Method

apply(long value)

apply(long value)

apply(long value)

Method Logic

Returns a long value

Returns a long value

Returns a long value

Conclusion

There are several methods available to facilitate your use of the LongFunction interface. These methods permit you to create personalized functions that accept lengthy values as arguments and produce output of various sorts. You can apply these functions in diverse scenarios. Ranging from data filtering to mapping and collection reduction.

Updated on: 28-Jul-2023

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements