import java.util.Comparator;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @ClassName MyFunction
* @Author Javan
* @Date 2021/6/30 14:54
* @Description
*
* Function: 接收一个参数 object
* Consumer: 接收一个参数 void
* Predicate: 接收一个参数 boolean
* UnaryOperator: 接收一个参数 object
* BinaryOperator: 接收两个参数 object
*
**/
public class MyFunction {
public interface MyConsumer{
void accept(T t,U u,R r,S s,G g);
static MyConsumer print() {
return (p1,p2,p3,p4,p5) -> System.err.println("MyConsumer print -> " + Stream.of(p1,p2,p3,p4,p5).map(String::valueOf).collect(Collectors.joining(",")));
}
}
private static Function f = ((Function) t -> t)
.andThen(t -> {
System.err.println(t);
return t + "then -> ";
}).andThen(t -> {
System.err.println(t);
return t + "then1 -> ";
}).compose(d -> {
System.err.println(d);
return d + "compose -> ";
}).compose(d -> {
System.err.println(d);
return d + "compose1 -> ";
});
/**
* 接收两个参数返回一个参数
*/
static BiFunction bif = ((BiFunction) (s, i) -> 0l).andThen(t -> {
System.err.println(t);
return t + "then -> ";
});
/**
* 消费者
* 接收一个参数,然后执行方法体,没有返回值.
*/
static Consumer c = (d) -> System.err.println("Consumer -> " + d);
static BiConsumer bc = (b, i) -> System.err.println("BiConsumer -> " + b + "-" + i);
static DoubleConsumer bbc = b -> System.err.println("DoubleConsumer -> " + b);
static IntConsumer ic = b -> System.err.println("IntConsumer -> " + b);
static LongConsumer lc = b -> System.err.println("LongConsumer -> " + b);
static MyConsumer mc = (p1,p2,p3,p4,p5) -> System.err.println("MyConsumer -> " + Stream.of(p1,p2,p3,p4,p5).map(String::valueOf).collect(Collectors.joining(",")));
/**
* 谓词
* 接收一个参数,返回true or false.
* 就像汉语中的谓词,
* 例如:
* "猫是动物"一句中的"是"就是一个谓词,而"猫"是客体。
* cat -> animal.containsKey(cat);
*
* "3 大于 2"中"大于"是一个谓词。
* num -> 3>2;
*
* 3+1等于5
*
* num -> 3+1 == 5;
*/
static Predicate p = (d) -> true;
static BiPredicate bp = (s, i) -> true;
/**
* 一元运算符
*/
static UnaryOperator up = d -> d;
/**
* 二元运算符
*/
static BinaryOperator bo = (s, v) -> v;
static BinaryOperator minBo = BinaryOperator.minBy(Comparator.reverseOrder());
static BinaryOperator maxBo = BinaryOperator.maxBy(Comparator.reverseOrder());
static DoubleBinaryOperator dbo = (s, v) -> v;
static IntBinaryOperator ibo = (s, v) -> v;
static LongBinaryOperator lbo = (s, v) -> v;
/**
* 布尔值供应商
* 不接受参数,在主体进行操作返回true or false.
*/
static Supplier
--------------------Function
start
start compose1 ->
start compose1 -> compose ->
start compose1 -> compose -> then ->
--------------------Predicate
Predicate -> true
BiPredicate -> true
--------------------UnaryOperator
1
--------------------BinaryOperator
BinaryOperator -> 2
minBy -> 2
maxBy -> 111
DoubleBinaryOperator -> 2.0
IntegerBinaryOperator -> 2
LongBinaryOperator -> 2
--------------------Supplier
Supplier -> java.lang.Object@3532ec19
BooleanSupplier -> true
IntSupplier -> 1
DoubleSupplier -> 1.0
LongSupplier -> 1
--------------------Consumer
Consumer -> 1
BiConsumer -> 1-2
DoubleConsumer -> 1.0
IntConsumer -> 1
LongConsumer -> 1
MyConsumer -> 1,2,3,4,5
MyConsumer print -> One,two,three,four,five
--------------------compare
true
Process finished with exit code 0