ToLongBiFunction<T,U>是来自java.util.function函数包裹。此函数接口接受两个引用类型参数作为输入,并生成长值结果。ToLongBiFunction<T,U>接口可以用作lambda表达式或方法引用的赋值目标,并且只包含一个抽象方法:applyAsLong()。
@FunctionalInterface interface ToLongBiFunction<T, U> { long applyAsLong(T t, U u); }
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 电话号码: "+ result); name = "Adithya"; System.out.println("Adithya's 电话号码: "+ getMobileNum.applyAsLong(mobileNumbers, name)); name = "Jai"; System.out.println("Jai's 电话号码: "+ getMobileNum.applyAsLong(mobileNumbers, name)); name = "Chaitanya"; System.out.println("Chaitanya's 电话号码: "+ getMobileNum.applyAsLong(mobileNumbers, name)); } }
输出结果
Raja's 电话号码: 9959984805 Adithya's 电话号码: 7702144433 Jai's 电话号码: 7013536286 Chaitanya's 电话号码: 9652671506