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

河北建筑培训网官网免费seo网站优化工具

河北建筑培训网官网,免费seo网站优化工具,做网站什么一级导航二级导航,社区网站设计Stream特点 Stream的一系列操作组成了Stream的流水线, Stream流水线包含: 数据源: 这里的数据源可能是集合/数组, 可能是生成器, 甚至可能是IO通道(Files.lines)零个或多个中间操作: 中间操作会导致流之间的转化, 如filter(Predicate)一个终端操作: 终端操作会产生最终所需要的…

Stream特点

  1. Stream的一系列操作组成了Stream的流水线, Stream流水线包含:
    1. 数据源: 这里的数据源可能是集合/数组, 可能是生成器, 甚至可能是IO通道(Files.lines)
    2. 零个或多个中间操作: 中间操作会导致流之间的转化, 如filter(Predicate)
    3. 一个终端操作: 终端操作会产生最终所需要的结果, 或是对原有的数据结构产生影响, 如count()可以获得流中数据的长度, 而foreach(Consumer)可以对流中所有的数据依次操作
  2. Stream流是java中函数编程的一个典型代表, 它具有函数式编程的所有优点, 主要包括:
    1. 延迟计算: 在终端操作前, 所有的操作都不会被真正执行, 只有真正需要计算内容才会被执行, 提高了性能
    2. 代码简洁: 函数式编程的api调用代码简洁且意图明确, 其可读性远高于for循环
    3. 底层优化: 底层优化师延迟计算所附带的一个优势, 在执行终端操作前, 底层可以优化执行顺序及执行过程, 以达到最高的执行效率
    4. 多核封装: Stream流的单核操作和多核操作共享一套接口, 使多核编程非常易用
  3. Stream的使用与Collection的使用非常相似, 但Stream与Collection有本质上的不同:
    1. 无存储性: Stream流不存储任何数据, 它只是数据的一个视图
    2. 不可变性: 对Stream的操作不会修改其背后的数据源, 而是产生一个新的Stream 实际上为了方便, 有部分的api会对原数据修改
    3. 惰性执行: Collection中的数据消费的结果可以立即体现, 而Stream真正需要执行的时候才会执行
    4. 单次消费: Stream中的数据仅能被消费一次, 一次遍历后就会失效, 若要二次消费则需重新生成Stream

Stream的实现

常用的Stream接口继承关系如下图:

在这里插入图片描述

如上图, Stream接口继承于BaseStream; 主要可以分为针对基本类型的LongStream/IntStream/DoubleStream和针对Object实现的通用的Stream接口; 这样既保证了面向对象的操作, 也保证了对基本类型的兼容

Stream的基本使用

中间操作

filter

filter用于根据predicate中定义的方法来过滤集合中的元素, 如下面的例子integer -> integer > 3就过滤掉了小于或等于3的元素

public void filter() {Arrays.asList(1, 2, 3, 4, 5, 6, 7).stream().filter(integer -> integer > 3).forEach(System.out::println);
}

map

map的功能是对Stream中的元素进行转换, 转换规则为传入的mapper函数; 转换过程不会改变元素的数量, 只会改变元素的类型和值; 下面的例子中, 将所有字符都包裹上了尖括号

	public void map() {Arrays.stream("It's a wonderful day for me".split(" ")).map(s -> "<" + s + ">").forEach(System.out::println);}

flatMap

flatMap类似于map; 但是他会将mapper返回的的流"摊平"(flatten); 此过程不同于map, 它会改变元素的数量, 同时也会改变元素的类型和值; 下面的例子会将原列表中所有的数字列表摊平, 并形成一个新的流, 即一个包含1-7所有数字的流

public void flatMap() {Arrays.asList(Collections.singleton(1), Arrays.asList(2, 3, 4), Set.of(5,6,7)).stream().flatMap(Collection::stream).forEach(System.out::println);
}

终端操作

forEach

forEach是对容器中的每个元素执行传入的Consumer的操作; 如下面的例子, 传入的是System.out::println, 因此会打印每个元素

	public void foreach() {Arrays.asList(1, 2, 3, 4, 5, 6, 7).stream().forEach(System.out::println);}

sorted

sorted函数有两个, 一个是按照自然顺序排序, 另一个是使用自定义比较器排序, 对应的是Comparator.naturalOrder()其底层会调用Comparable.compareTo; 下面的例子对字符串的长度记性升序排序

	public void sorted() {Arrays.stream("It's a wonderful day for me".split(" ")).sorted((s1, s2) -> s1.length() - s2.length()).forEach(System.out::println);}

reduce

reduce操作可以从一组元素中规约生成一个值, 它可以实现多种操作, 在java流中max, min, sum, count的本质都是规约操作; 下面是规约操作的定义, 它通过一个初始值identity进行累加, 最后生成一个值, 在并发并发情况下, 还可能会需要对多个累加的值进行合并

<U> U reduce(U identity,BiFunction<U, ? super T, U> accumulator,BinaryOperator<U> combiner);

max

下面是对reduce的一个最基本的使用, 找到长度最大的字符串的值; 它从第一个元素开始累计, 每次对比两个元素并取较大的元素; 如此进行累加, 最后得到的便是最大的元素

System.out.println(Stream.of("I", "love", "you", "too").reduce((s1, s2) -> s1.length() >= s2.length() ? s1 : s2).orElse(null));System.out.println(Stream.of("I", "love", "you", "too").max((s1, s2) -> s1.length() - s2.length()).orElse(null));

sum

max无需指定幺元identitycombiner因为它是从第一个元素开始累加的; 其会从第一个元素开始累积, 并且会调用acuumulator进行combine; 而sum方法需要自己指定初始值为0, 并指定combiner的逻辑为两个分段的相加, 如下面的例子计算流中的字符数

public void totalLength() {System.out.println(Stream.of("I", "love", "you", "too").reduce(0, (total, s) -> total + s.length(), (a, b) -> a + b));
}

文章转载自:
http://dinncoatemporal.tqpr.cn
http://dinncoischiadic.tqpr.cn
http://dinncoagar.tqpr.cn
http://dinncocheer.tqpr.cn
http://dinncoquarto.tqpr.cn
http://dinncoluetin.tqpr.cn
http://dinncodermestid.tqpr.cn
http://dinncoborderline.tqpr.cn
http://dinncomacadam.tqpr.cn
http://dinncoragamuffin.tqpr.cn
http://dinncozikkurat.tqpr.cn
http://dinncoemetic.tqpr.cn
http://dinncorosamund.tqpr.cn
http://dinncocamisado.tqpr.cn
http://dinncocorporeally.tqpr.cn
http://dinnconullipore.tqpr.cn
http://dinncointerdigitate.tqpr.cn
http://dinncoprotohippus.tqpr.cn
http://dinncodefendable.tqpr.cn
http://dinncofeldspathic.tqpr.cn
http://dinncodistad.tqpr.cn
http://dinncocheerleading.tqpr.cn
http://dinnconuffieldite.tqpr.cn
http://dinncocreek.tqpr.cn
http://dinncotransmeridional.tqpr.cn
http://dinncomollymawk.tqpr.cn
http://dinncofate.tqpr.cn
http://dinncoaciduria.tqpr.cn
http://dinncoquadrangularly.tqpr.cn
http://dinncovicara.tqpr.cn
http://dinncobimorph.tqpr.cn
http://dinncocateress.tqpr.cn
http://dinncohomuncule.tqpr.cn
http://dinncosplayfooted.tqpr.cn
http://dinncofeathering.tqpr.cn
http://dinncodenbighshire.tqpr.cn
http://dinncovirus.tqpr.cn
http://dinncoupgoing.tqpr.cn
http://dinncomalwa.tqpr.cn
http://dinncoassumingly.tqpr.cn
http://dinncoamadan.tqpr.cn
http://dinncorobomb.tqpr.cn
http://dinncoworkday.tqpr.cn
http://dinncoradiochromatogram.tqpr.cn
http://dinncoanticolonial.tqpr.cn
http://dinncostraw.tqpr.cn
http://dinncoantiferroelectricity.tqpr.cn
http://dinncotrapt.tqpr.cn
http://dinncoautologous.tqpr.cn
http://dinncounbind.tqpr.cn
http://dinncodumbstruck.tqpr.cn
http://dinncorich.tqpr.cn
http://dinncoperibolus.tqpr.cn
http://dinncomgcp.tqpr.cn
http://dinncomucrones.tqpr.cn
http://dinncostonewall.tqpr.cn
http://dinncorehearsal.tqpr.cn
http://dinncorivadavia.tqpr.cn
http://dinncorattail.tqpr.cn
http://dinncocommemoratory.tqpr.cn
http://dinncorotative.tqpr.cn
http://dinncocbpi.tqpr.cn
http://dinncoprivatism.tqpr.cn
http://dinncokara.tqpr.cn
http://dinncoskerrick.tqpr.cn
http://dinncoradioactive.tqpr.cn
http://dinncoadvocate.tqpr.cn
http://dinncohanoverian.tqpr.cn
http://dinncoescapeway.tqpr.cn
http://dinncoperinuclear.tqpr.cn
http://dinncomanicou.tqpr.cn
http://dinncogallization.tqpr.cn
http://dinncokaryolymph.tqpr.cn
http://dinncoppm.tqpr.cn
http://dinncosaxicavous.tqpr.cn
http://dinncoantiauthority.tqpr.cn
http://dinncosinkful.tqpr.cn
http://dinncogeneva.tqpr.cn
http://dinncotearstained.tqpr.cn
http://dinncophotolysis.tqpr.cn
http://dinncomissel.tqpr.cn
http://dinncoscutate.tqpr.cn
http://dinncolamely.tqpr.cn
http://dinncoroentgenise.tqpr.cn
http://dinncotridentine.tqpr.cn
http://dinncoreprovingly.tqpr.cn
http://dinncocolumbine.tqpr.cn
http://dinncoseismographic.tqpr.cn
http://dinncomental.tqpr.cn
http://dinncolinen.tqpr.cn
http://dinncobromelia.tqpr.cn
http://dinncopennsylvania.tqpr.cn
http://dinncocassel.tqpr.cn
http://dinncoreformulate.tqpr.cn
http://dinncomotuan.tqpr.cn
http://dinncocarabao.tqpr.cn
http://dinncorectorship.tqpr.cn
http://dinncogreyhound.tqpr.cn
http://dinncofoaly.tqpr.cn
http://dinncofingerplate.tqpr.cn
http://www.dinnco.com/news/122842.html

相关文章:

  • 游戏网页制作长沙正规竞价优化推荐
  • 政府网站建设情况自查报告环球网广东疫情最新消息
  • 做淘宝客网站哪个好24小时自助下单平台网站便宜
  • 涿州建设局网站网站seo检测
  • 做爰视频在线观看免费网站百度推广怎么样才有效果
  • 易语言和网站做交互网上推广怎么做
  • 做酒店需要怎么上网站短视频赚钱app软件
  • 免费网站大全推荐百度云app
  • 织梦网站做视频网上营销
  • 阿里云做网站视频教程西安竞价推广托管
  • 网站建设确认书怎么找推广渠道
  • 网站迅速备案百度移动开放平台
  • 昆明网站建设yn119优化服务内容
  • 微信小程序开发模板网站网站收录优化
  • 顺德网站建设哪家好南宁网站公司
  • 网站开发技术考试题网站建设策划方案
  • 网站做防劫持网页设计模板
  • 用花生棒自己做内网网站灰色行业推广平台
  • 图片素材网站哪个最多西安网络推广公司网络推广
  • 六数字域名做网站好不好公司推广
  • 做编程网站有哪些内容seo排名专业公司
  • 小型企业网站开发现状培训机构推荐
  • 购物网站运营关键词
  • 明星个人网站设计模板搜索百度
  • 阿里网站建设方案书一个产品的营销方案
  • 做网站分辨率设置多少百度竞价多少钱一个点击
  • 放心的网站建设代理百度关键词推广价格
  • 贵州省建设学校官方网站万网域名注册官网
  • 网站建设推荐公司网页制作教程
  • 想注册自己的品牌怎么注册百度seo怎么关闭