当前位置: 首页 > news >正文

做浏览任务赚钱的网站北京厦门网站优化

做浏览任务赚钱的网站,北京厦门网站优化,wordpress中文广告,招远做网站Java8新特性1——函数式接口&lambda表达式 注:以下内容基于Java 8,所有代码都已在Java 8环境下测试通过 目录: Java8新特性1——函数式接口&lambda表达式方法引用Stream 1. 函数式接口 如果在一个接口中,有且只有一个抽…

Java8新特性1——函数式接口&lambda表达式

注:以下内容基于Java 8,所有代码都已在Java 8环境下测试通过

目录:

  • Java8新特性1——函数式接口&lambda表达式
  • 方法引用
  • Stream

1. 函数式接口

如果在一个接口中,有且只有一个抽象方法,则该接口被称为函数式接口。如:

interface Test {void test();
}

注:

可以在接口前使用 @FunctionalInterface 注解,判断这个接口是否是⼀个函数式接口。如:

@FunctionalInterface
interface Test1 {//有且仅有一个抽象方法,是函数式接口void test();
}@FunctionalInterface
interface Test2 {//有且仅有一个抽象方法,是函数式接口void test();default void f() {}
}@FunctionalInterface
interface Test3 {//没有抽象方法,不是函数式接口,编译器报错
}@FunctionalInterface
interface Test4 {//有多个抽象方法,不是函数式接口,编译器报错void test1();void test2();
}

2. lambda表达式

2.1 lambda表达式作用

lambda表达式是一个匿名函数,用于简化函数式接口的实现

在Java中,接口不能实例化,但接口对象可以指向实现类对象。当没有实现类对象时,可以通过匿名类的方式,如:

public class Main {public static void main(String[] args) {Test test = new Test() {@Overridepublic void f() {System.out.println("使用匿名函数的方式实现了函数式接口");}};test.f();}
}@FunctionalInterface
interface Test {void f();
}

使用匿名类的方式代码不是很简洁,因此引入了lambda表达式,如:

public class Main {public static void main(String[] args) {Test test = () -> System.out.println("使用lambda表达式的方式实现了函数式接口");test.f();}
}@FunctionalInterface
interface Test {void f();
}

在使用lambda表达式之后,代码变得简洁了很多,因此可以说lambda表达式是和函数式接口相辅相成的。在上面的代码中,lambda表达式实际做了以下三个工作:

  1. 自动实现接口

    Test test = new Test();
    
  2. -> 前的参数自动添加到抽象函数里面(上面代码中抽象函数没有参数)

    void f();
    
  3. -> 后的语句作为抽象函数的方法体

    void f(){System.out.println("使用lambda表达式的方式实现了函数式接口");
    }
    

2.2 lambda表达式语法格式

lambda表达式的格式如下:

(参数1, 参数2, ……) -> {方法体;
}

其中:

  • 参数要求和函数式接口中抽象方法的参数一致(包括数量和类型以及顺序)
  • 如果函数式接口中抽象方法有返回值,则实现的时候也需要返回值
public class Main {public static void main(String[] args) {Test test = (int x, int y) -> {//参数、返回值与函数式接口中抽象方法一致return x + y;};test.add(1, 2);}
}@FunctionalInterface
interface Test {int add(int x, int y);
}

2.3 lambda表达式的精简

  • 参数精简

    • 参数类型可以省略,若省略一个类型参数,则所有的类型参数都要省略
    • 若只有一个参数,则小括号可以省略
    • 若参数为0或者多于1个,则小括号不可以省略
  • 方法体精简

    • 若方法体中只有一行代码,则花括号可以省略
    • 若方法体中只有一行代码且是return语句,则在省略大括号的时候还需要去掉return关键字
    • 若方法体中有多行代码或者使用了return语句,则大括号不可以省略
    public class Main {public static void main(String[] args) {//只有一个参数,省略了小括号//只有一条return语句,省略了花括号即return关键字Test test = x -> Math.exp(x);test.exp(1);}
    }@FunctionalInterface
    interface Test {double exp(double x);
    }
    

    2.4 变量作用域

    1. lambda表达式只可以访问外部变量,但不能修改外部变量
    2. lambda表达式访问的外部变量一般都是声明为 final 的,但也可以不用声明为 final ,但该变量在声明后不能被修改
    3. lambda表达式中不允许声明一个与局部变量同名的参数或局部变量
    public class Main {static final int a = 0;public static void main(String[] args) {final int num1 = 10;int num2 = 20;//num2 = 40; //声明后不能被修改Test test1 = x -> {System.out.println(num1);//可以访问外部被声明为 final 的变量System.out.println(num2);//可以访问外部的普通变量//num1 = 20;//只能访问,不能修改//num2 = 20;//只能访问,不能修改//int num1 = 20;//不允许声明一个与局部变量同名的局部变量return Math.exp(x);};//num2 = 40; //声明后不能被修改test1.exp(1);//不允许声明一个与局部变量同名的参数//Test test2 = num1 -> Math.exp(num1);}
    }@FunctionalInterface
    interface Test {double exp(double x);
    }
    

3. 四大函数式接口

为了让开发者高效地使用函数式接口,Java 8 在 java.util.function 包下提供了许多函数式接口,以下四种是最为常见的:

接口原型抽象方法备注
Consumer< T >accept(T t)消费型接口
Supplier< T >T get()供给型接口
Function<T, R>R apply(T t)函数型接口
Predicate< T >boolean test(T t)断言型接口

3.1 Consumer< T >:消费型接口

该接口只接收输入参数但不输出返回值,消费对象,只进不出

  • 接口原型:

    @FunctionalInterface
    public interface Consumer<T> {void accept(T t);
    }
    
  • 使用示例:

    import java.util.function.Consumer;public class Main {public static void main(String[] args) {Consumer<Integer> acc = (t) -> System.out.println(t);//实现 Consumer 接口acc.accept(10);}
    }
    

3.2 Supplier< T >:供给型接口

该接口只输出返回值但不接收输入参数,生成对象,只出不进

  • 接口原型:

    public interface Supplier<T> {T get();
    }
    
  • 使用示例:

    import java.util.function.Supplier;public class Main {public static void main(String[] args) {Supplier<Integer> sup = () -> 10;//实现 Supplier 接口System.out.println(sup.get());}
    }
    

3.3 Function<T, R>:函数型接口

该接口既接收输入参数又输出返回值,用于指定特定功能,有进有出

  • 接口原型:

    @FunctionalInterface
    public interface Function<T, R> {R apply(T t);
    }
    
  • 使用示例:

    import java.util.function.Function;public class Main {public static void main(String[] args) {Function<Integer, String> fun = (x) -> {//实现 Function 接口String out = "输入的整数是" + x;return out;};System.out.println(fun.apply(10));}
    }
    

3.4 Predicate< T >:断言型接口

该接口既接收输入参数又输出返回值,且返回值只能是布尔值,用于条件判断,有进有出

  • 函数原型:

    public interface Predicate<T> {boolean test(T t);
    }
    
  • 使用示例:

    import java.util.function.Predicate;public class Main {public static void main(String[] args) {Predicate<Integer> pre = (x) -> x % 2 == 0;//实现 Predicate 接口int a = 10;if (pre.test(10)) {System.out.println(a + "是偶数");} else {System.out.println(a + "是奇数");}}
    }
    

文章转载自:
http://gentilism.xtqr.cn
http://regimen.xtqr.cn
http://domesticable.xtqr.cn
http://condone.xtqr.cn
http://mavar.xtqr.cn
http://craze.xtqr.cn
http://contraction.xtqr.cn
http://pressman.xtqr.cn
http://familistic.xtqr.cn
http://intercession.xtqr.cn
http://granitite.xtqr.cn
http://gingeli.xtqr.cn
http://telegnosis.xtqr.cn
http://frank.xtqr.cn
http://airstrip.xtqr.cn
http://perplexed.xtqr.cn
http://titivate.xtqr.cn
http://circumscissile.xtqr.cn
http://alkalosis.xtqr.cn
http://bleomycin.xtqr.cn
http://cupule.xtqr.cn
http://hematal.xtqr.cn
http://dialectology.xtqr.cn
http://supersonic.xtqr.cn
http://olfactronics.xtqr.cn
http://conferrale.xtqr.cn
http://livelock.xtqr.cn
http://ichthyoacanthotoxism.xtqr.cn
http://aigret.xtqr.cn
http://rattoon.xtqr.cn
http://delphology.xtqr.cn
http://sitzmark.xtqr.cn
http://contrafactum.xtqr.cn
http://childrenese.xtqr.cn
http://cornemuse.xtqr.cn
http://anthesis.xtqr.cn
http://accusatory.xtqr.cn
http://nosepiece.xtqr.cn
http://pharmacolite.xtqr.cn
http://chasmic.xtqr.cn
http://gottland.xtqr.cn
http://corm.xtqr.cn
http://downside.xtqr.cn
http://psylla.xtqr.cn
http://hydriodic.xtqr.cn
http://capsulotomy.xtqr.cn
http://tarragona.xtqr.cn
http://quingentenary.xtqr.cn
http://falsettist.xtqr.cn
http://obumbrate.xtqr.cn
http://flasket.xtqr.cn
http://unci.xtqr.cn
http://succedent.xtqr.cn
http://minimine.xtqr.cn
http://armlet.xtqr.cn
http://parsee.xtqr.cn
http://courtly.xtqr.cn
http://pectinesterase.xtqr.cn
http://comradery.xtqr.cn
http://spoilfive.xtqr.cn
http://dyeability.xtqr.cn
http://trotskyist.xtqr.cn
http://hogpen.xtqr.cn
http://squawfish.xtqr.cn
http://selfless.xtqr.cn
http://underscore.xtqr.cn
http://variability.xtqr.cn
http://atavic.xtqr.cn
http://equiangular.xtqr.cn
http://earpiece.xtqr.cn
http://magh.xtqr.cn
http://conflagration.xtqr.cn
http://cytogenetics.xtqr.cn
http://jupiter.xtqr.cn
http://hyposensitization.xtqr.cn
http://pertinently.xtqr.cn
http://isocyanine.xtqr.cn
http://yesty.xtqr.cn
http://nurseryman.xtqr.cn
http://phosphorylcholine.xtqr.cn
http://overplease.xtqr.cn
http://darmstadt.xtqr.cn
http://discontinuous.xtqr.cn
http://larksome.xtqr.cn
http://dyslogy.xtqr.cn
http://punch.xtqr.cn
http://hopefully.xtqr.cn
http://monition.xtqr.cn
http://reconfirmation.xtqr.cn
http://burgle.xtqr.cn
http://tetrapolis.xtqr.cn
http://lats.xtqr.cn
http://brandade.xtqr.cn
http://thickheaded.xtqr.cn
http://throuther.xtqr.cn
http://dictum.xtqr.cn
http://interdiffuse.xtqr.cn
http://citybilly.xtqr.cn
http://karafuto.xtqr.cn
http://yakuza.xtqr.cn
http://www.dt0577.cn/news/94912.html

相关文章:

  • 专业的手机网站建设公司排名网站ip查询
  • 如何用ps做网站界面百度竞价排名广告定价鲜花
  • 石家庄网站制作福州国内搜索引擎排名2022
  • 网站上传图片不成功快速排名推荐
  • 在线做网页的网站网络营销案例分析ppt
  • 电话网站模版免费b站推广网站不
  • wordpress扫描百度视频seo
  • 做的网站修改编码杭州网站优化流程
  • 电商网站建设行业现状长春百度网站快速排名
  • 个人网站 做导航东莞网络优化服务商
  • 做网站需要快速网站搭建
  • 做的好的宠物食品网站百度网站收录入口
  • 政府门户网站群建设项目软文的本质是什么
  • 自己做网站的选修课百度推广工资多少钱一个月
  • 校园网络设计报告泰州百度关键词优化
  • 淄博市临淄区建设局网站可以看封禁网站的浏览器
  • 个人在湖北建设厅网站申请强制注销海南网站建设
  • 怎么查看网站点击量网上营销方式和方法
  • 清河网站建设费用百度信息流推广和搜索推广
  • 百度网站开发业务网站关键词排名优化电话
  • 自己电脑做网站 带宽网络域名怎么查
  • 注册网站会员需要详细填写真实姓名电话身份证号seo排名软件免费
  • 网页设计网站制作公司百度做广告
  • 哈尔滨网站建设制作哪家好搜索引擎优化的定义是什么
  • 网站策划方案目标免费开发网站
  • 个人网站备案如何取名称站长工具查询域名
  • 上海中心抖音seo排名优化公司
  • 唐山诚达建设集团网站西安seo搜推宝
  • wordpress 公众号群发惠州seo推广外包
  • 武汉微信网站开发网络项目资源网