链式编程+流式计算
- 函数式型接口 Function<T,R> 传入一个参数,获取一个返回值
- 断定型接口 Predicate 传入一个参数,返回一个 bool 值
- 消费型接口 Consumer 传入参数,没有返回值
- 供给型接口 Supplier 没有传参数,有返回值
Package java.util.function
package com.xiao.test;
import com.sun.media.jfxmediaimpl.HostUtils;
import java.sql.Connection;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class FunctionTest {
public static void main(String[] args) {
predicate();
}
private static void predicate() {
Predicate<String> predicate = new Predicate<String>() {
@Override
public boolean test(String s) {
if (s.equals("zhangxiao")){
return true;
}else{
return false;
}
}
};
boolean xx = predicate.test("zhangxiao");
Predicate<String> predicate1 = (s)->{
if (s.equals("zhangxiao")){
return true;
}else{
return false;
}
};
boolean xiao = predicate1.test("xiao");
System.out.println(xiao);
}
private static void supplier() {
Supplier<String> supplier = new Supplier<String>() {
@Override
public String get() {
return "hello supplier";
}
};
String s = supplier.get();
Supplier<String> supplier1 = () -> {
return "hello supplier lambda";
};
String s1 = supplier1.get();
System.out.println(s1);
}
private static void function() {
Function<String, String> function = new Function<String, String>() {
@Override
public String apply(String s) {
if (s.equals("zhang")) {
return "zhangxiao";
} else {
return "error";
}
}
};
String zh = function.apply("zhang");
Function<String, String> function1 = (x) -> {
if (x.equals("zhang")) {
return "zhangxiao";
} else {
return "error";
}
};
String er = function1.apply("zhang");
System.out.println(er);
}
public static void consumer() {
Consumer<String> consumer = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
};
consumer.accept("zhang");
Consumer<String> result = (s) -> {
System.out.println(s);
};
result.accept("abc");
}
}