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
How to implement ToLongBiFunction<T, U> using lambda expression in Java?
ToLongBiFunction<T, U> is a in-built functional interface from java.util.function package. This functional interface accepts two reference type parameters as input and produces a long-valued result. ToLongBiFunction<T, U> interface can be used as an assignment target for a lambda expression or method reference and contains only one abstract method: applyAsLong().
Syntax
@FunctionalInterface
interface ToLongBiFunction<T, U> {
long applyAsLong(T t, U u);
}
Example
import java.util.*;
import java.util.function.ToLongBiFunction;
public class ToLongBiFunctionTest {
public static void main(String[] args) {
ToLongBiFunction<Map<String, Long>, String> getMobileNum = (map,value) -> { // lambda
if(map != null && !map.isEmpty()) {
if(map.containsKey(value)) {
return map.get(value);
}
}
return 0;
};
Map<String, Long> mobileNumbers = new HashMap<String, Long>();
mobileNumbers.put("Raja", 9959984805L);
mobileNumbers.put("Adithya", 7702144433L);
mobileNumbers.put("Jai", 7013536286L);
mobileNumbers.put("Chaitanya", 9652671506L);
String name = "Raja";
long result = getMobileNum.applyAsLong(mobileNumbers, name);
System.out.println("Raja's Mobile no: "+ result);
name = "Adithya";
System.out.println("Adithya's Mobile no: "+ getMobileNum.applyAsLong(mobileNumbers, name));
name = "Jai";
System.out.println("Jai's Mobile no: "+ getMobileNum.applyAsLong(mobileNumbers, name));
name = "Chaitanya";
System.out.println("Chaitanya's Mobile no: "+ getMobileNum.applyAsLong(mobileNumbers, name));
}
}
Output
Raja's Mobile no: 9959984805 Adithya's Mobile no: 7702144433 Jai's Mobile no: 7013536286 Chaitanya's Mobile no: 9652671506
Advertisements