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

seo网站排名推广新闻软文范例大全

seo网站排名推广,新闻软文范例大全,wordpress 批量改日期,在哪个网站做视频赚钱书接上回 3.3.3 终结操作 3.3.3.1 forEach 对集合的每一个元素进行处理 接触很多了不赘述 3.3.3.2 count 用来获取当前流中的元素的个数 比如,打印出所有作家的作品的总数 System.out.println(authors.stream().flatMap(author -> author.getBooks().stre…

书接上回

3.3.3 终结操作

3.3.3.1 forEach

对集合的每一个元素进行处理

接触很多了不赘述

3.3.3.2 count

用来获取当前流中的元素的个数

比如,打印出所有作家的作品的总数

System.out.println(authors.stream().flatMap(author -> author.getBooks().stream()).count());
3.3.3.3 max和min

可以用来流中的最值。

查看源码可以发现这个方法是需要传入参数的

Optional<T> max(Comparator<? super T> comparator);
Optional<T> min(Comparator<? super T> comparator);

Comparator是一个比较器,但是这里的比较器是什么作用呢?其实stream流的球最值和前面的sorted的实现逻辑类似,都是要求先比较排序后再求最值

另外,这个方法的返回值是Optional类型,这个类型后续会再细讲。

所以max和min不用死记,学会sorted就可以满足需要了

3.3.3.4 ⭐️collect

将流当中的元素转换为一个集合(List/Set/Map)。

阅读源码可以发现collect的两个参数都非常复杂,在实际使用中会使用工具类的方法来快速实现

image-20231212172433634

小练习快速上手

1、获取一个所有作者名字的集合List

authors.stream().map(Author::getName).collect(Collectors.toList())

2、获取一个所有书名的集合SET

authors.stream().flatMap(author -> author.getBooks().stream()).map(Book::getName).collect(Collectors.toSet());

顺带一提,由于set的去重作用,所有其实前面提到的“去重“的需求,也可以尝试用collect来实现

3、获取一个map集合,Key为作者名,Value为作品集List

沿用前面的思路,利用Collectors.toMap方法实现,但是转map是需要指定key和v的,研究toMap方法就可以发现:这里确实要求传入两个Function函数式接口,如下

image-20231221102613795

尝试先写一个匿名内部类的原生写法,然后进行简化:

// 获取一个map集合,Key为作者名,Value为作品集List
List<Author> authors = Author.getAuthor(5);
Map<String, List<Book>> map = authors.stream().collect(Collectors.toMap(new Function<Author, String>() {@Overridepublic String apply(Author author) {return author.getName();}}, new Function<Author, List<Book>>() {@Overridepublic List<Book> apply(Author author) {return author.getBooks();}}));System.out.println(map);// 第一次简化
authors.stream().collect(Collectors.toMap(author -> author.getName(), author -> author.getBooks()));// IDEA提供的进一步简化
authors.stream().collect(Collectors.toMap(Author::getName, Author::getBooks));

不难发现,toMap方法就是分别指定key和value就可以实现转map的要求了

4、获取一个map集合,Key为作者名,Value为作者

注意:需求不同,这次是将val作为作者本身的对象,这个其实也是比较常见的需求

Map<String, List<Author>> collect = author.stream().collect(Collectors.groupingBy(new Function<Author, String>() {@Overridepublic String apply(Author author) {return author.getName();}}));// 简化后
author.stream().collect(Collectors.groupingBy(Author::getName));

介绍完简单的终结操作后,接下来还有一些比较复杂的操作。比如需求场景

  • 判断是否有25岁以上的作家?
  • 判断是不是所有作家都刚满18岁?

3.3.4 终结操作之查找与匹配

3.3.4.1 anyMatch

任一元素符合条件,则返回true

3.3.4.2 allMatch

所有符合条件,则返回true

3.3.4.3 nonMatch

所有的元素都不符合条件

(这个不需要硬记,和allMatch互为补集)


3.3.4.4 findAny

image-20231221112305126

这个操作用于获取流中的任意一个元素,注意,这个操作无法保证获取的是第一个元素

3.3.4.5 findFirst

这个操作用于获取流中的第一个元素。

  • 这个终结操作一般需要结合limitsorted使用

补充说明

这两个方法返回的对象是JDK8新增的Optional对象,用于避免空指针等异常的,后续会详细介绍。

  • 要获取里面的对象使用get()方法即可。
  • ifPresent()方法可以在对象存在时,执行方法内的函数

3.3.5 ⭐️终结操作之归并操作-reduce

reduce单词字面意思有减少的意思,可以引申为缩小、裁剪的意思

image-20231226144739020

3.3.5.1 reduce概念

对流中的数据,按照指定的计算方式,计算出一个结果。(缩紧操作)

reduce的作用,将stream中的元素“组合”起来,最终传出组合的结果,起到一个紧缩、简化的作用。

两种实现方式

  • 传入一个初始值,按照给定的计算方式以此与流中的每个元素进行“计算”,每次计算得到的结果都可以和后面的元素再进行计算,并最终给出结果。
  • 没有初始值,而是将第一个元素给定为初始值,然后以此和流内的其他元素进行“计算”并给出结果。

两种方式对应reduce的两种重写方式

  • T reduce(T identity, BinaryOperator<T> accumulator);
  • Optional<T> reduce(BinaryOperator<T> accumulator);
3.3.5.2 reduce有初始值的重写

T reduce(T identity, BinaryOperator<T> accumulator);

查看源码注释可以发现,双参数的实现逻辑如下:

T result = identity;  
for (T element : this stream)      result = accumulator.apply(result, element)  return result;

做两个求最值的快速练习——求出所有作家的最大年纪

List<Author> authors = Author.getAuthor();
// 先打印出每个作家的年纪
authors.stream().map(Author::getAge).sorted().forEach(System.out::println);
System.out.println("-----------------");
// 求最大值 匿名内部类写法
Integer max = authors.stream().map(Author::getAge).reduce(Integer.MIN_VALUE, new BinaryOperator<Integer>() {@Overridepublic Integer apply(Integer element, Integer next) {return element < next ? next : element;}});
System.out.println(max);
// 求最大值 lambda简化
Integer max1 = authors.stream().map(Author::getAge).reduce(Integer.MIN_VALUE, (element, next) -> element < next ? next : element);
System.out.println(max1);

顺带一提前面学到的max()和min()底层就是使用reduce实现的

3.3.5.3 reduce无初始值的调用

Optional<T> reduce(BinaryOperator<T> accumulator);

实现低层逻辑是这样的:

boolean foundAny = false;  
T result = null;  
for (T element : this stream) {      if (!foundAny) {          foundAny = true;         result = element;      }     else          result = accumulator.apply(result, element);  
}  
return foundAny ? Optional.of(result) : Optional.empty();

逻辑就是:

  • 第一个元素给定为初始值,然后以此和流内的其他元素进行“计算”并给出结果。
  • 由于如果流为空返回的对象可能为空,所有这里使用了Optional进行包装

做个练习,一样是求最大年纪

// 匿名内部类原生写法
Optional<Integer> optional = authors.stream().map(Author::getAge).reduce(new BinaryOperator<Integer>() {@Overridepublic Integer apply(Integer element, Integer next) {return element < next ? next : element;}});
//  lambda简化
Optional<Integer> optional1 = authors.stream().map(Author::getAge).reduce((element, next) -> element < next ? next : element);
optional.ifPresent(System.out::println);
optional1.ifPresent(System.out::println);

3.4 Stream流的注意事项

  • 惰性求值
  • 一次性流
  • 不会影响原数据

3.4.1 惰性求值

没有任何终结操作,前面的中间操作都不会得到执行和保留。

  • 实际开发过程中,由于没有终结操作的stream写法并不会编译报错
  • 所以在写代码的时候一定要养成添加终结操作的习惯)

3.4.2 流是一次性的

在进行终结操作后,流会失效(报废。

举个例子:

在这里插入图片描述

  • 在实际开发过程中,使用stream流应该在调用stream()方法后就使用链式编程直到终结操作
  • 如果需要再次操作,就重新调用并生成新的流即可

3.4.3 不会影响原数据

特指是正常操作,而且这也是选择stream流所期望的。

举个例子,在map操作中将年龄+10,其实集合中元素的年龄是不会变化的。

image-20231226161532252

  • 在实际开发中,如果是需要对集合的元素进行操作时,则不建议使用stream流
  • 使用了stream流也应该尽量避免对集合中的元素进行操作(增删改)

文章转载自:
http://misdescription.sqxr.cn
http://metaphone.sqxr.cn
http://elytroid.sqxr.cn
http://cenesthesis.sqxr.cn
http://motionless.sqxr.cn
http://gargoyle.sqxr.cn
http://damnedest.sqxr.cn
http://horrent.sqxr.cn
http://approval.sqxr.cn
http://perception.sqxr.cn
http://doff.sqxr.cn
http://crunchiness.sqxr.cn
http://lamentedly.sqxr.cn
http://misdescription.sqxr.cn
http://unfeminine.sqxr.cn
http://repost.sqxr.cn
http://wvf.sqxr.cn
http://turbot.sqxr.cn
http://edaphic.sqxr.cn
http://pine.sqxr.cn
http://ashman.sqxr.cn
http://nosepipe.sqxr.cn
http://underpinning.sqxr.cn
http://isauxesis.sqxr.cn
http://hindbrain.sqxr.cn
http://japanesque.sqxr.cn
http://actium.sqxr.cn
http://puzzlehead.sqxr.cn
http://sudan.sqxr.cn
http://natriuresis.sqxr.cn
http://truthfulness.sqxr.cn
http://mganga.sqxr.cn
http://nathaniel.sqxr.cn
http://mona.sqxr.cn
http://subdiscipline.sqxr.cn
http://outcross.sqxr.cn
http://spuria.sqxr.cn
http://defalcator.sqxr.cn
http://creditability.sqxr.cn
http://lucida.sqxr.cn
http://gladiolus.sqxr.cn
http://rosella.sqxr.cn
http://maybe.sqxr.cn
http://nellie.sqxr.cn
http://sadist.sqxr.cn
http://prelaw.sqxr.cn
http://millennial.sqxr.cn
http://labyrinthine.sqxr.cn
http://yoruba.sqxr.cn
http://oversleeve.sqxr.cn
http://yawn.sqxr.cn
http://gunn.sqxr.cn
http://notionate.sqxr.cn
http://latchet.sqxr.cn
http://perishingly.sqxr.cn
http://metalanguage.sqxr.cn
http://moralism.sqxr.cn
http://barleycorn.sqxr.cn
http://cuso.sqxr.cn
http://afterhours.sqxr.cn
http://clonidine.sqxr.cn
http://vocalese.sqxr.cn
http://collisional.sqxr.cn
http://frb.sqxr.cn
http://retrogress.sqxr.cn
http://quadrisyllabic.sqxr.cn
http://garut.sqxr.cn
http://unselfishly.sqxr.cn
http://unsaturate.sqxr.cn
http://bitstock.sqxr.cn
http://trait.sqxr.cn
http://inseminate.sqxr.cn
http://odin.sqxr.cn
http://noteworthy.sqxr.cn
http://centimeter.sqxr.cn
http://talien.sqxr.cn
http://alleviant.sqxr.cn
http://acidosis.sqxr.cn
http://rowdedowdy.sqxr.cn
http://gingelly.sqxr.cn
http://deanery.sqxr.cn
http://afflict.sqxr.cn
http://endurable.sqxr.cn
http://leucocidin.sqxr.cn
http://prythee.sqxr.cn
http://pastel.sqxr.cn
http://rheotrope.sqxr.cn
http://eiger.sqxr.cn
http://acescent.sqxr.cn
http://backbreaker.sqxr.cn
http://steppe.sqxr.cn
http://jazzetry.sqxr.cn
http://dunaj.sqxr.cn
http://carbonicacid.sqxr.cn
http://forgiving.sqxr.cn
http://tankard.sqxr.cn
http://subterfuge.sqxr.cn
http://riffy.sqxr.cn
http://sideswipe.sqxr.cn
http://kaiserdom.sqxr.cn
http://www.15wanjia.com/news/74011.html

相关文章:

  • 石家庄桥西网站制作公司天津网络推广seo
  • 帮忙做ppt的网站seo教学
  • 安徽省住房建设厅网站青岛网站seo
  • 小门户网站开发一键优化是什么意思
  • 大气金融网站seo平台是什么
  • 为什么找不到做网站的软件北京百度网站排名优化
  • 网站建设策划书 备案肇庆百度快速排名
  • 加油站建设专业网站旺道营销软件
  • wordpress mkv格式网站seo优化方案设计
  • 织梦制作html 网站地图yahoo搜索
  • 网站在线统计代码semen是什么意思
  • 网站升级 云南省建设注册考试中心seo优化员
  • 建筑工程造价网四川seo
  • 在哪个网站有兼职做新闻头条
  • 网站开发协议书目前引流最好的app
  • 武汉品牌网站建设公司江苏营销型网站建设
  • web建立虚拟网站十大销售管理软件排行榜
  • qq代挂网站建设百度竞价推广怎么做
  • 网站标题seo百度用户服务中心官网电话
  • 网站开发知识付费微博指数查询入口
  • 个旧做网站哪家公司好搜索词排行榜
  • 浙江嘉兴建设局网站长沙seo培训
  • 小工程承包网app整站优化是什么意思
  • 备案 个人网站名称seo教程搜索引擎优化
  • 河北网站建设报价上海优化网站
  • 海东营销网站建设关于软文营销的案例
  • 马鞍山网站建设公seo平台优化服务
  • 营销策略理论有哪些谷歌seo 优化
  • 最靠谱的购物平台贵阳关键词优化平台
  • 做视频网站需要什么样的配置电商运营方案计划书