网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么使用java8中双冒号

本篇内容主要讲解“怎么使用java8中双冒号”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用java8中双冒号”吧!

成都创新互联公司服务项目包括鞍山网站建设、鞍山网站制作、鞍山网页制作以及鞍山网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,鞍山网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到鞍山省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

代码其实很简单:

以前的代码一般是如此的:

public class AcceptMethod {  public static void printValur(String str){ System.out.println("print value : "+str); }  public static void main(String[] args) { List al = Arrays.asList("a","b","c","d"); for (String a: al) {  AcceptMethod.printValur(a); } //下面的for each循环和上面的循环是等价的  al.forEach(x->{  AcceptMethod.printValur(x); }); }}

现在JDK双冒号是:

public class MyTest { public static void printValur(String str){ System.out.println("print value : "+str); }  public static void main(String[] args) { List al = Arrays.asList("a", "b", "c", "d"); al.forEach(AcceptMethod::printValur); //下面的方法和上面等价的 Consumer methodParam = AcceptMethod::printValur; //方法参数 al.forEach(x -> methodParam.accept(x));//方法执行accept }}

上面的所有方法执行玩的结果都是如下:

print value : aprint value : bprint value : cprint value : d

在JDK8中,接口Iterable 8中默认实现了forEach方法,调用了 JDK8中增加的接口Consumer内的accept方法,执行传入的方法参数。

JDK源码如下:

/** * Performs the given action for each element of the {@code Iterable} * until all elements have been processed or the action throws an * exception. Unless otherwise specified by the implementing class, * actions are performed in the order of iteration (if an iteration order * is specified). Exceptions thrown by the action are relayed to the * caller. * * @implSpec *

The default implementation behaves as if: *

{@code * for (T t : this) *  action.accept(t); * }
* * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */ default void forEach(Consumer action) { Objects.requireNonNull(action); for (T t : this) {  action.accept(t); } }

另外补充一下,JDK8改动的,在接口里面可以有默认实现,就是在接口前加上default,实现这个接口的函数对于默认实现的方法可以不用再实现了。类似的还有static方法。现在这种接口除了上面提到的,还有BiConsumer,BiFunction,BinaryOperation等,在java.util.function包下的接口,大多数都有,后缀为Supplier的接口没有和别的少数接口。

到此,相信大家对“怎么使用java8中双冒号”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


新闻名称:怎么使用java8中双冒号
地址分享:http://bjjierui.cn/article/pgpehj.html

其他资讯