内置函数式接口
1.Consumer<T>:消费型接口
void accept(T t);
2.Supplier<T>: 供给型接口
T get();
3.Function<T, R>:函数型接口
R apply(T t);
4.Predicate<T>:断言型接口
boolean test(T t);
public class TestJava84 {
//消费型接口
@Test
public void test01(){
happy(100d,m->{
System.out.println("今天吃饭,花了" + m + "元钱");
});
}
public void happy(double money, Consumer con){
con.accept(money);
}
//供给型接口
@Test
public void test02(){
System.out.println(getList(10, () -> {
return (int) (Math.random() * 100);
}));
}
public List<Integer> getList(int num, Supplier<Integer> sup){
List<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
list.add(sup.get());
}
return list;
}
//函数型接口
@Test
public void test03(){
System.out.println(operationString("wangmeng", x -> x.toUpperCase()));
}
public String operationString(String str, Function<String,String> fun){
return fun.apply(str);
}
//断言型接口
@Test
public void test04(){
System.out.println(operationBoolean(1, x -> x > 0));
}
public Boolean operationBoolean(int num, Predicate<Integer> pre){
return pre.test(num);
}
}

本文详细介绍了Java8中内置的四种主要函数式接口:Consumer(消费型接口)、Supplier(供给型接口)、Function(函数型接口)和Predicate(断言型接口)。通过具体实例演示了如何使用这些接口进行函数式编程,包括处理字符串、生成随机数列表和执行条件判断。
2566

被折叠的 条评论
为什么被折叠?



