- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
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
Approach 3: Using Pre Defined Function
To use a predefined function that implements the LongFunction
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