Home  >  Article  >  Java  >  LongFunction interface in Java

LongFunction interface in Java

WBOY
WBOYforward
2023-09-15 12:53:06901browse

LongFunction interface in Java

LongFunction in Java is a functional interface that accepts a parameter of type long and returns a result of some other type. A functional interface is an interface with only one abstract method. To first use this interface, you must import the java.util.function package.

The function method of this interface is "apply()", which takes a long value as a parameter and returns the function result represented by R.

grammar

R apply(long value);

algorithm

In order to effectively implement this functional interface in your work, there are three definition steps that must be followed beforehand.

  • First, setting up an instance of such an implementation requires defining a suitable lambda expression that can accurately preview what will be used in its "apply" function when a long type variable or argument is provided.

  • After setting up a specific algorithm via step 1 here, proceed to step 2 - calling "apply" on said instance while adding the long type argument inputs respectively. Once steps 1 and 2 have been satisfactorily executed, it's time for our third and final step - to use all the output values ​​from the previous steps within the available area in our ongoing code.

Method 1: Define Lambda 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

illustrate

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

Method 2: Using method references

You can use method references to implement the apply(long value) method of 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

illustrate

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

Method 3: Use predefined functions

To use a predefined function that implements the LongFunction interface, you create an instance of the interface and pass the 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

illustrate

In the above sqrt predefined function, LongFunction is implemented.

Comparison between methods

standard

method 1

Method 2

Method 3

type

Lamda expression

Method reference

Predefined functions

method

Application (long value)

Application (long value)

Application (long value)

Method logic

Return long value

Return long value

Return long value

in conclusion

There are several ways to help you use the LongFunction interface. These methods allow you to create personalized functions that accept lengthy values ​​as arguments and produce various types of output. You can apply these features in different scenarios. From data filtering to mapping and collection reduction.

The above is the detailed content of LongFunction interface in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete