
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement LongFunction Using Lambda and Method Reference in Java
LongFunction<R> is an in-built functional interface defined in java.util.function package. This functional interface expects a long-valued parameter as input and produces a result. LongFunction<R> interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: apply().
Syntax
@FunctionalInterface public interface LongFunction<R> { R apply(long value) }
Example
import java.util.function.LongFunction; public class LongFunctionTest { public static void main(String[] args) { LongFunction<Long> function1 = (long i) -> { // lambda expression return i + i; }; System.out.println("Using Lambda Expression: " + function1.apply(10)); LongFunction<Long> function2 = LongFunctionTest::add; // method reference System.out.println("Usimg Method Reference: " + function2.apply(20)); } static long add(long i) { return i + i; } }
Output
Using Lambda Expression: 20 Usimg Method Reference: 40
Advertisements