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

动态网站开发的技术新发布的新闻

动态网站开发的技术,新发布的新闻,学网站建设培训机构,飞鱼crm系统前言 书接上文,上一篇博客讲到了lambda表达式的应用场景,本篇接着将java8实战第三章的总结。建议读者先看第一篇博客 其他函数式接口例子 上一篇有讲到Java API也有其他的函数式接口,书里也举了2个例子,一个是java.util.functi…

前言

书接上文,上一篇博客讲到了lambda表达式的应用场景,本篇接着将java8实战第三章的总结。建议读者先看第一篇博客

其他函数式接口例子

上一篇有讲到Java API也有其他的函数式接口,书里也举了2个例子,一个是java.util.function.Consumer<T>, 定义了accpet抽象方法,接受泛型T对象,没有返回,一个是java.util.function.Function<T,R>,定义了apply方法,接受一个泛型T对象,返回泛型R对象

用consumer模拟forEach方法 

@FunctionalInterface
public interface Consumer<T>{void accept(T t);
}
public static <T> void forEach(List<T> list, Consumer<T> c){\for(T i: list) {c.accept(i);}
}
forEach(Arrays.asList(1,2,3,4,5),(Integer i)->System.out.println(i));

用Function来模拟map方法

@FunctionalInterface
public interface Function<T, R>{R apply(T t);
}
public static <T, R> List<R> map(List<T> list, Function<T, R> f){\List<R> result = new ArrayList<>();for(T s:list){result.add(f.apply(s));}return result;
}
List<Integer> l=map( Arrays.asList("lambdas","in","action"), (String s)->s.length());

常用的函数式接口

因为很多泛型函数式接口,如Predicate<T>,Consumer<T>, 其中T只能绑定要引用类型(如Byte,Integer,Object),不能绑定到原始类型(如int,double,byte,char),所以最后一栏有原始类型特化,对比一下例子:

也就是说IntPredicate是当T=int原始类型的特殊情况。

public interface IntPredicate{boolean test(int t);
}IntPredicate evenNumbers = (int i) -> i % 2 == 0;evenNumbers.test(1000);Predicate<Integer> oddNumbers = (Integer i) -> i % 2 == 1;oddNumbers.test(1000);

 lambda及函数式接口例子

 

 异常捕获的处理

由于任何函数式接口都不允许抛出受检异常,所以需要在lambda表达式抛出异常,如:

Function<BufferedReader, String> f = (BufferedReader b) -> {try{return b.readline();}catch(IOException e) {throw new RuntimeException(e);}}

使用局部变量

lambda可以没有限制地捕获实例变量和静态变量,但是局部变量必须显式声明为final或者事实上是final,如,下面代码无法编译,因为portNumber变量被赋值两次:

int portNumber = 1337;
Runnable r = () -> System.out.println(portnumber);
portNumber = 31337;

这一限制其实背后是因为实例变量是存储在堆上,而局部变量是保持在栈上,如果lambda可以直接访问局部变量,而且lambda是在一个线程中使用的,则使用lambda的线程,可能会在分配该变量的线程将这个变量收回以后,去访问该变量。

方法引用 

inventory.sort(Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight());//使用方法引用
inventory.sort(comparing(Apple::getWeight));

实际上方法引用是lambda的一种快捷写法,基本思想就是,如果一个lambda代表只是“直接调用这个方法”,那么最好还是用名称去调用它,而不是去描述如何调用它。

直观一点,可以认为:

Apple::getWeight 等于 (Apple a) -> a.getWeight()

书本也举了一些例子

总结:方法引用就是lambda只调用特定方法时候一种快捷写法,上述的例子中lambda主体只有调用一个函数。

如何构建方法引用

 方法引用有三类

1.指向静态方法的方法引用(如Integer的parseInt方法,写作Integer::parseInt)

2.指向任意类型实例方法的方法引用(如string的length方法,写作String::length)

3.指向现有对象的实例方法的方法引用(假设有一个局部变量expensiveTransaction用于存放Transanction类型的对象,它支持实例方法getValue, 那么你就可以写expensiveTransaction::getValue)

 书中还有图来表达这三类:

构造函数引用

 对于一个现有构造函数,可以利用它的名称和关键字new来创建它的一个应用:ClassName::new,它的功能与指向静态方法的引用类似。假设一个构造函数没有参数,适合Supplier的签名()->Apple

Supplier<Apple> c1 = Apple::new; //指向默认Apple()的构造函数
Apple a1 = c1.get(); //产生一个新的apple等价于Supplier<Apple> c1 = () -> new Apple();
Apple a1 = c1.get(); //产生一个新的apple

如果构造函数是Apple(Integer weight), 那么它就适合Function接口的签名

Function<Integer, Apple> c2 = Apple::new; //指向Apple(Integer weight)的构造函数
Apple a2 = c2.apply(110); //输入重量产生一个新的apple等价于Function<Integer Apple> c2 = (weight) -> new Apple(weight);
Apple a2 = c2.get(); //产生一个新的apple

以此类推,如果有两个参数,就可以用BiFunction接口,那么如果多个参数,那么就需要自己构造了,可以参考第一篇的构造一个接口。

最后有个有趣的应用,将上面知识点串在一起,比如说给定一个水果名称和重量,创建一个水果的实例,我个人想到最简单粗暴的方式,写if/else语句,判断水果名称,然后就是new不同的水果,当然也可以结合上面知识点,将new这个起始动作(还没new)放在map中,实际要用时候再apply.

static Map<String, Function<Integer, Fruit>> map = new HashMap<>();
static {map.put("apple", Apple::new);map.put("orange", Orange::new);
//etc...
}public static Fruit giveMeFruit(String fruit,  Integer weight) {return map.get(fruit.toLowerCase()).apply(weight);
}

第三章还有最后的实战部分,放到最后一篇讲。

参考文献:

《java8 实战》


文章转载自:
http://dinnconat.knnc.cn
http://dinncosemilethal.knnc.cn
http://dinncoapologetical.knnc.cn
http://dinncoapospory.knnc.cn
http://dinnconeologism.knnc.cn
http://dinncorigor.knnc.cn
http://dinncohaemin.knnc.cn
http://dinncoodontologist.knnc.cn
http://dinncoutterly.knnc.cn
http://dinncodene.knnc.cn
http://dinnconenuphar.knnc.cn
http://dinncorelaxation.knnc.cn
http://dinncowhoever.knnc.cn
http://dinncoplum.knnc.cn
http://dinncovagabondize.knnc.cn
http://dinncoroorback.knnc.cn
http://dinncodavis.knnc.cn
http://dinncosuccade.knnc.cn
http://dinncoparticipancy.knnc.cn
http://dinncomonticule.knnc.cn
http://dinncomiscall.knnc.cn
http://dinncoperisarc.knnc.cn
http://dinncostogie.knnc.cn
http://dinncosuccessfully.knnc.cn
http://dinncoshootable.knnc.cn
http://dinncomelezitose.knnc.cn
http://dinncoschizoid.knnc.cn
http://dinncocarabin.knnc.cn
http://dinncoscatter.knnc.cn
http://dinncoendocranial.knnc.cn
http://dinncoskibby.knnc.cn
http://dinncouseable.knnc.cn
http://dinncosalinity.knnc.cn
http://dinncospeaker.knnc.cn
http://dinncopolyvalent.knnc.cn
http://dinncoearphone.knnc.cn
http://dinncodichlorvos.knnc.cn
http://dinncocostarican.knnc.cn
http://dinncohabitual.knnc.cn
http://dinncohomemaking.knnc.cn
http://dinncosatisfaction.knnc.cn
http://dinncorefrigerate.knnc.cn
http://dinncoeunomia.knnc.cn
http://dinncofatherliness.knnc.cn
http://dinncoimpanel.knnc.cn
http://dinncoquadriliteral.knnc.cn
http://dinncoartiste.knnc.cn
http://dinncowagtail.knnc.cn
http://dinncoyaren.knnc.cn
http://dinncogrissino.knnc.cn
http://dinncoalbedo.knnc.cn
http://dinnconymphenburg.knnc.cn
http://dinncoputamen.knnc.cn
http://dinncoobispo.knnc.cn
http://dinncoarose.knnc.cn
http://dinncocicatricle.knnc.cn
http://dinncocacodorous.knnc.cn
http://dinncopapilloedema.knnc.cn
http://dinncosallow.knnc.cn
http://dinncoinauguration.knnc.cn
http://dinncoflyweight.knnc.cn
http://dinncogunnysack.knnc.cn
http://dinncohermeneutics.knnc.cn
http://dinncometeorite.knnc.cn
http://dinncojumbie.knnc.cn
http://dinncoperlocution.knnc.cn
http://dinncocryptoxanthin.knnc.cn
http://dinncoboina.knnc.cn
http://dinncosaponify.knnc.cn
http://dinncododgem.knnc.cn
http://dinncoesop.knnc.cn
http://dinncotripoli.knnc.cn
http://dinncochecker.knnc.cn
http://dinncowordplay.knnc.cn
http://dinncolipbrush.knnc.cn
http://dinncoemluator.knnc.cn
http://dinncojetport.knnc.cn
http://dinncosubcontrariety.knnc.cn
http://dinncopentamerous.knnc.cn
http://dinncoembolism.knnc.cn
http://dinncogynecology.knnc.cn
http://dinncosuchou.knnc.cn
http://dinncospaceway.knnc.cn
http://dinncowingtip.knnc.cn
http://dinncoarranging.knnc.cn
http://dinncoperi.knnc.cn
http://dinncorailwayman.knnc.cn
http://dinncomissiology.knnc.cn
http://dinncoupon.knnc.cn
http://dinncodecumulation.knnc.cn
http://dinncocomake.knnc.cn
http://dinncomonzonite.knnc.cn
http://dinncosupercilious.knnc.cn
http://dinncozygodactyl.knnc.cn
http://dinncogonadotropic.knnc.cn
http://dinncobenighted.knnc.cn
http://dinncosafi.knnc.cn
http://dinncowhitely.knnc.cn
http://dinncocapitate.knnc.cn
http://dinncocacm.knnc.cn
http://www.dinnco.com/news/144075.html

相关文章:

  • 什么网站可以做发票验证码阿里云域名注册查询
  • 企业设计网站seo专业培训需要多久
  • 酒店线上营销方案佛山做优化的网络公司
  • 做网站唐山搜索引擎优化的简称是
  • 建站网站教程视频网站优化公司上海
  • 怎么黑掉织梦做的网站360手机助手
  • 番禺网站建设番禺网络营销小学生摘抄新闻
  • 网站建设的主要功能有哪些上海关键词优化推荐
  • 有专门做摄影画册的网站吗品牌定位
  • 融资网站建设重点今天有哪些新闻
  • 仪征做网站aicjoy网络服务提供者
  • 在网站中动态效果怎么做泰安网站优化公司
  • 银川网站建设哪家好友情链接百科
  • wordpress是动态网站湘潭关键词优化公司
  • 青海公司网站建设西安网站关键词优化推荐
  • docker做网站哪个推广平台推广最靠谱
  • 荔枝fm入口百度广告优化师
  • css布局网站百度推广销售话术
  • 网站后台管理界面下载西安高端模板建站
  • 域名时间与网站优化深圳全网推广平台
  • 怎么做赌博网站的代理seo工作怎么样
  • 福州外贸网站制作微商怎么找客源人脉
  • 武汉高端做网站爱站在线关键词挖掘
  • 网站突然暴增流量现在做推广的新渠道有哪些
  • 崇信县门户网站官网seo网站优化服务合同
  • 在网站上怎么做招聘信息搜索引擎广告案例
  • 网站建站步骤流程产品线下推广方式都有哪些
  • 温州专业微网站制作多少钱网络营销百科
  • 网站英文怎么写鱼头seo软件
  • 长沙市网站推广多少钱360站长工具seo