函数,接收一个或多个参数,返回指定类型结果
@Data
public static class Foo {
String strVal = "Hello";
int intVal = 1;
Double doubleVal = 2.0;
Long longVal = 3L;
Boolean trueVal = true;
String strTmp;
int intTmp = 1;
Double doubleTmp;
Long longTmp;
}
可以指定输入和输出类型。
Function<Integer, String> function = (intV) -> intV + "";
assertEquals("1", function.apply(foo.intVal));
不需要指定输入类型,只需要指定输出类型。
DoubleFunction<String> doubleFunction = (d1) -> d1 + "!";
assertEquals("123.0!", doubleFunction.apply(123.0));
IntFunction<String> intFunction = (d1) -> d1 + "!";
assertEquals(foo.intVal + "!", intFunction.apply(foo.intVal));
LongFunction<String> longFunction = (d1) -> d1 + "!";
assertEquals(foo.longVal + "!", longFunction.apply(foo.longVal));
输入类型需要指定。
ToDoubleFunction<Foo> toDoubleFunction = (f1) -> f1.doubleVal;
assertEquals(foo.doubleVal, foo.doubleVal, toDoubleFunction.applyAsDouble(foo));
ToIntFunction<Foo> toIntFunction = (f1) -> f1.intVal;
assertEquals(foo.intVal, toIntFunction.applyAsInt(foo));
ToLongFunction<Foo> toLongFunction = (f1) -> f1.longVal;
assertEquals(foo.longVal.longValue(), toLongFunction.applyAsLong(foo));
无需指定输入输出类型。
DoubleToIntFunction doubleToIntFunction = (d1) -> foo.intVal;
assertEquals(foo.intVal, doubleToIntFunction.applyAsInt(99.0));
DoubleToLongFunction doubleToLongFunction = (d1) -> foo.longVal;
assertEquals(foo.longVal.longValue(), doubleToLongFunction.applyAsLong(1.2));
IntToDoubleFunction intToDoubleFunction = (d1) -> foo.doubleVal;
assertEquals( foo.doubleVal, foo.doubleVal, intToDoubleFunction.applyAsDouble(1));
IntToLongFunction intToLongFunction = (d1) -> foo.longVal;
assertEquals(foo.longVal.longValue(), intToLongFunction.applyAsLong(1));
LongToDoubleFunction longToDoubleFunction = (d1) -> foo.doubleVal;
assertEquals(foo.doubleVal, foo.doubleVal, longToDoubleFunction.applyAsDouble(1));
LongToIntFunction longToIntFunction = (d1) -> foo.intVal;
assertEquals(foo.intVal, longToIntFunction.applyAsInt(1));
可以指定输入,输出类型,可以进行运行返回数据结果。
BiFunction<String, Integer, String> biFunction = (s1, s2) -> s1 + s2;
assertEquals("Hello123", biFunction.apply("Hello", 123));
只需要定义输入的参数,输出类型固定。
ToDoubleBiFunction<Foo, Foo> doubleBiFunction = (f1, f2) -> f1.doubleVal + f2.doubleVal;
assertEquals(foo.doubleVal *2, foo.doubleVal *2, doubleBiFunction.applyAsDouble(foo, foo));
ToIntBiFunction<Foo, Foo> intBiFunction = (f1, f2) -> f1.intVal + f2.intVal;
assertEquals(foo.intVal *2, foo.intVal *2, intBiFunction.applyAsInt(foo, foo));
ToLongBiFunction<Foo, Foo> longBiFunction = (f1, f2) -> f1.longVal + f2.longVal;
assertEquals(foo.longVal *2, foo.longVal *2, longBiFunction.applyAsLong(foo, foo));