@FunctionalInterface
public interface AppleInterface {
public void test();
}
java.util.function.Consumer; java.util.function.Function; java.util.function.Predicate; java.util.function.Supplier;
FunctionInterface interface=null;
public void filter(FunctionInterface interface)
{
interface.doSomeThing();
}
public class Apple {
private String color;
private float weight;
public Apple(String color, float weight) {
this.color = color;
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
@FunctionalInterface
public interface AppleInterface {
public void test();
}
@Test
public void DefineFunctionInterface(){
//自定义函数式接口
AppleInterface at=()->System.out.println("define FunctionInterface AppleInterface.");
at.test();
}
@Test
public void ConsumerTest(){
Consumer<Apple> consumer=(Apple app)->{System.out.println(app.getColor()+","+app.getWeight());};
List<Apple> apps=Arrays.asList(new Apple("red", 120),new Apple("blue", 80),
new Apple("green",100));
ConsumerApple(apps,consumer);
}
public void ConsumerApple(List<Apple> apps,Consumer<Apple> c){
for(Apple app:apps){
c.accept(app);
}
}
@Test
public void SupplierTest(){
Supplier<Apple> supplier=()->{return new Apple("hello supplier",999);};
Apple app=supplier.get();
System.out.println(app.getColor()+","+app.getWeight());
}
@Test
public void PredicateTest(){
//系统预定义函数式接口测试
Predicate<Apple> p1=(Apple a)->{if(a.getWeight()>90) return true;return false;};
Predicate<Apple> p2=(Apple a)->{if(a.getColor().equals("blue")) return true;return false;};
List<Apple> apps=Arrays.asList(new Apple("red", 120),new Apple("blue", 80),
new Apple("green",100));
filterApple(apps,p1);//筛选重量大于90g的苹果
filterApple(apps,p2);//筛选蓝色的苹果
}
public void filterApple(List<Apple> apps,Predicate<Apple> p){
for(Apple app:apps){
if(p.test(app)){
System.out.println(app.getColor()+","+app.getWeight());
}
}
}
@Test
public void FunctionTest(){
Function<String,Apple> function=(String s)->{return new Apple(s,666);};
Apple app=function.apply("red");
System.out.println(app.getColor()+","+app.getWeight());
app=function.apply("green");
System.out.println(app.getColor()+","+app.getWeight());
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有