public static void main(String[] args) {
//SpringApplication.run(Application.class);
// ======================================= Consumer =============================================
// == 类命中的Bi表示二元操作,也就是实现的接口要传入2个参数
// ==============================================================================================
Consumer<String> consumer = (s)->{System.out.println("消费一个" + s);};// 消费一个给的值
consumer.accept("world");
BiConsumer<Integer,Integer> biConsumerAfter = (x,y)->{System.out.println("我是after的accept"); };
BiConsumer<Integer,Integer> biConsumer = (x,y)->{ System.out.println("我不是after的accept"); };
biConsumer.andThen(biConsumerAfter).accept(1,2);
IntConsumer intConsumer = (x)->{System.out.println("消费一个int值:" + x);};
intConsumer.accept(1);
LongConsumer longConsumer = (l)->{System.out.println("消费一个long类型的值" + l);};
longConsumer.accept(1);
ObjDoubleConsumer<String> objDoubleConsumer = (t,d)->{System.out.println("消费传进来的Object和Double的值" + t + d);};
objDoubleConsumer.accept("hello",1.0);
ObjIntConsumer<String> objIntConsumer = (t,d)->{System.out.println("消费传进来的Object和Int的值" + t + d);};
objIntConsumer.accept("hello",1);
ObjLongConsumer<String> objLongConsumer = (t,d)->{System.out.println("消费传进来的Object和Long的值" + t + d);};
objLongConsumer.accept("hello",1L);
// ======================================= Supplier =============================================
// == Supplier:生产者接口,主要生成不同类型的值
// ==============================================================================================
BooleanSupplier booleanSupplier = ()->{return true;};
System.out.println(booleanSupplier.getAsBoolean());
DoubleSupplier doubleSupplier = ()->{return 1.0;};
System.out.println("double值生产者,生产了一个double值:" + doubleSupplier.getAsDouble());
IntSupplier intSupplier = ()->{return 1;};
System.out.println("int类型的生产者,生产一个int类型的值:" + intSupplier.getAsInt());
LongSupplier longSupplier = ()->{return 1;};
System.out.println("Long值生产者生产了一个long值:" + longSupplier.getAsLong());
Supplier<String> supplier = ()->{return "hello";};
System.out.println("生产者生产了一个单词:" + supplier.get());
// ======================================= Predicate =============================================
// == Predicate:根据传入的参数,返回一个boolean值
// == 类名中有Bi的表示二元操作,也就是实现的接口要传入2个参数
// ==============================================================================================
Predicate<String> predicate = (s)->{return s.equals("hello") ? true : false;};
System.out.println("predicate来判断是否的结果为:" + predicate.test("world"));
IntPredicate intPredicate = (i)->{return i == 1 ? true : false;};
System.out.println("输入的int值是否为1:" + intPredicate.test(97));
LongPredicate longPredicate = (l)->{ return l == Long.MAX_VALUE ? true : false;};
System.out.println("给定给一个long值,返回一个boolean值:" + longPredicate.test(Long.MAX_VALUE));
DoublePredicate doublePredicate = (x)->{
if(Double.valueOf(x) instanceof Double){ return true;}
return false;
};
System.out.println(doublePredicate.test(1.0));
BiPredicate<Integer,Integer> biPredicate = (x,y)->{return x > y ? true : false;};
System.out.println(biPredicate.test(1,2));
// ======================================= BiFunction =============================================
// == BiFunction:二元操作,也就是实现的接口要传入2个参数,通过两个参数制作出一个值
// ==============================================================================================
BiFunction<Integer,Integer,String> biFunction = (x,y)->{return String.valueOf(x+y);};
System.out.println(biFunction.apply(1,2));
// Bi就是Binary(二元运算的二元的意思)
ToDoubleBiFunction<Character,Integer> toDoubleBiFunction = (t,u)->{return t + u;};//二元运算,通过2个数算出一个double
System.out.println("二元运算根据T和U得出的double值为:" + toDoubleBiFunction.applyAsDouble('a',1));
ToIntBiFunction<Character,Integer> toIntBiFunction = (t,u)->{return t+ u;};
System.out.println("二元运算根据T和U得出的int值为:" + toIntBiFunction.applyAsInt('b',2));
ToLongBiFunction<Character,Integer> toLongBiFunction = (t,u)->{return t+ u;};
System.out.println("二元运算根据T和U得出的Long值为:" + toLongBiFunction.applyAsLong('b',2));
// ======================================= BinaryOperator =============================================
// == BinaryOperator:二元操作,也就是实现的接口要传入2个参数,通过两个参数制作出一个值
// ==============================================================================================
// BinaryOperator:二元操作了 UnaryOperator:一元操作
Comparator<Integer> comparator = (x,y)->{return x-y;};
System.out.println(BinaryOperator.maxBy(comparator).apply(5,2));// 根据比较器,将输入的2个数返回一个结果
DoubleBinaryOperator doubleBinaryOperator = (x,y)->{return 0;};
System.out.println(doubleBinaryOperator.applyAsDouble(1,3));// 给2个double的数,生成一个double的数
IntBinaryOperator intBinaryOperator = (left,right)->{return left + right;};
System.out.println("二位运算,计算2个int相加的值:" + intBinaryOperator.applyAsInt(1,2));
LongBinaryOperator longBinaryOperator = (left,right)->{return left + right;};
System.out.println("long类型二元操作,输入2个long值,返回一个新的long值:" + longBinaryOperator.applyAsLong(1,2));
// ======================================= Function =============================================
// == Function:
// ==============================================================================================
Function<Character,Integer> function = (t)->{int r = t.charValue();return r;};// 输入个T(Character),返回个R(Integer)
System.out.println("输入个T(Character),返回R(Integer)的值:" + function.apply('a'));
ToDoubleFunction<Character> toDoubleFunction = (t)->{return t;};
System.out.println("将一个T(Character)生成一个Double的值" + toDoubleFunction.applyAsDouble('a'));
ToIntFunction<Character> toIntFunction = (t)->{return t;};
System.out.println("将一个T(Character)生成一个int的值" + toIntFunction.applyAsInt('a'));
ToLongFunction<Character> toLongFunction = (t)->{return t;};
System.out.println("将一个T(Character)生成一个Long的值" + toLongFunction.applyAsLong('a'));
DoubleFunction<String> doubleFunction = (x)->{return x + "1";};//专门用来处理一个double类型的值,处理后的返回类型与类中写的对应
System.out.println(doubleFunction.apply(1));// 1.01
DoubleToIntFunction doubleToIntFunction = (x)->{return 1;};
System.out.println("输入一个double值,返回一个int值:" + doubleToIntFunction.applyAsInt(1.0));
DoubleToLongFunction doubleToLongFunction = (x)->{return 2L;};
System.out.println("输入一个double值,返回一个long值:" + doubleToLongFunction.applyAsLong(2.0));
//将int的值,转换为一个字母
IntFunction<String> intFunction = (x)->{return "bb";};
System.out.println("输入的int转换为的字母为:" + intFunction.apply(97));
IntToDoubleFunction intToDoubleFunction = (i)->{ return (double)i;};
System.out.println("讲一个int值转为double值为:" + intToDoubleFunction.applyAsDouble(1));
IntToLongFunction intToLongFunction = (i)->{ return (long)i;};
System.out.println("讲一个int值转为long值为:" + intToLongFunction.applyAsLong(1));
LongFunction<String> longFunction = (t)->{return "将Long类型的值" + t + "转为给的的R类型(String)返回";};
longFunction.apply(2);
LongToDoubleFunction longToDoubleFunction=(l)->{return (double)l;};
System.out.println("给一个long值,变为double类型 :"+ longToDoubleFunction.applyAsDouble(1L));
LongToIntFunction longToIntFunction = (l)->{return (int)l;};
System.out.println("给一个long值,变为int类型 :"+ longToIntFunction.applyAsInt(1));
// ======================================= UnaryOperator =============================================
// == UnaryOperator: 一元操做,就是接口都是一个参数。
// ==============================================================================================
// 传入一个值,得到同一类型的一个值
UnaryOperator<String> unaryOperator = (s)->{return s+s; };
System.out.println("传入一个值,得到同一类型的一个值:" + unaryOperator.apply("a"));
IntUnaryOperator intUnaryOperator = (i)->{return i+i;};
System.out.println("int一元操作,输入一个int值,返回一个int值为:" + intUnaryOperator.applyAsInt(2));
DoubleUnaryOperator doubleUnaryOperator = (x)->{return Double.valueOf(x);};
System.out.println("输入一个double值,返回一个double值:" + doubleUnaryOperator.applyAsDouble(2.0));
LongUnaryOperator longUnaryOperator = (x)->{return x+x;};
System.out.println("对long类型进行一元操作,操作数x的值为:" + longUnaryOperator.applyAsLong(10));
}
版权声明:本文为liuhaibo_ljf原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。