1.常见类型及使用
代码语言:javascript复制import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
/**
* @author
*
*/
public class FunctionTest {
/**
* @param args
*/
public static void main(String[] args) {
// Consumer 代表一个输入
Consumer<String> c2 = (a) -> {System.out.println("11打算参数:" a);};
Consumer<String> c3 = a -> {System.out.println("22打算参数:" a);};
Consumer<String> c4 = (String a) -> {
//具体的业务逻辑
System.out.println("33打算参数:" a);
};
c2.accept("这是一个入参参数啊111"); //消费
c3.accept("这是一个入参参数啊222"); //消费
c4.accept("这是一个入参参数啊333"); //消费
//输入2个参数,入参数据类型可以不一样
// BiConsumer 代表两个输入
BiConsumer<String,Integer> bc =(x,y) -> {
System.out.println("x y" x y);
};
bc.accept("中国",100);
//输出
// Supplier 代表一个输出
Supplier<String> s1 = () -> "hello";
Supplier<String> s2 = () -> {
//具体的业务逻辑
return "hello22";
};
System.out.println("supplier1=" s1.get());
System.out.println("supplier2=" s2.get());
//输入,输出参数类型不同
// Function 代表一个输入,一个输出(一般输入和输出是不同类型的) 也可以相同
Function<String, Integer> f1 = (str) -> Integer.valueOf(str);
Function<String, Integer> f2 = (String str) -> Integer.valueOf(str);
Function<String, Integer> f3 = (String str) -> {
//具体的业务逻辑
return Integer.valueOf(str);
};
Function<String, String> f4 = (str) -> "hello " str;
System.out.println("f1=" f1.apply("200"));
System.out.println("f2=" f2.apply("300"));
System.out.println("f3=" f3.apply("400"));
System.out.println("f4=" f4.apply("中国"));
//输入输出参数相同
//unary一元操作
// UnaryOperator 代表一个输入,一个输出(输入和输出是相同类型的)
UnaryOperator<String> u1 = (str) -> "hello" str;
System.out.println("u1=" u1.apply("chinese"));
//BiFunction 代表两个输入,一个输出(一般输入和输出是不同类型的)
BiFunction<String,String,Integer> bf = (x,y) -> x.length() y.length();
int length = bf.apply("abc","def");
System.out.println("字符串长度=" length);
//BinaryOperator 代表两个输入,一个输出(输入和输出是相同类型的)
//二元操作
BinaryOperator<String> bo = (x,y) -> x y;
String str = bo.apply("中国", "上海");
System.out.println(str);
}
}
2.固定类型及使用
代码语言:javascript复制import java.util.function.BiPredicate;
import java.util.function.BooleanSupplier;
import java.util.function.DoubleBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.LongFunction;
import java.util.function.Predicate;
/**
* @author
*
*/
public class Function2Test {
/**
* @param args
*/
public static void main(String[] args) {
//判断返回boolean
BiPredicate<Integer, Integer> bp = (x,y) -> {return x y == 100;};
System.out.println(bp.test(20, 80));
System.out.println(bp.test(20, 81));
//输出 布尔值
BooleanSupplier bs = () -> true;
BooleanSupplier bs2 = () -> false;
System.out.println(bs.getAsBoolean());
System.out.println(bs2.getAsBoolean());
//固定类型
DoubleBinaryOperator dbo = (x,y) -> x y;
System.out.println(dbo.applyAsDouble(1.0, 2.0));
//固定类型
IntConsumer ic = (x) -> {System.out.println(x 100);};
ic.accept(500);
//固定类型
LongFunction<String> lf = (x) -> String.valueOf(x);
System.out.println(lf.apply(1000l));
Predicate<Integer> p = (x) -> {return x == 100;};
System.out.println(p.test(100));
System.out.println(p.test(200));
}
}