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

长沙精品网站建设公司萧山seo

长沙精品网站建设公司,萧山seo,中国电信全渠道运营中心,WordPress防止垃圾注册文章目录引入流流的操作中间操作终端操作流的使用谓词筛选筛选各异的元素流的切片截断流跳过元素映射流的扁平化查找和匹配归约元素求和、最大值和最小值数值流构建流由值构建流由数组创建流引入流 java api提供的一种利用声明式的方式处理数据集合的一个东西,可以…

文章目录

    • 引入流
      • 流的操作
      • 中间操作
      • 终端操作
    • 流的使用
      • 谓词筛选
      • 筛选各异的元素
      • 流的切片
      • 截断流
      • 跳过元素
      • 映射
      • 流的扁平化
      • 查找和匹配
    • 归约
      • 元素求和、最大值和最小值
      • 数值流
      • 构建流
        • 由值构建流
        • 由数组创建流

引入流

  • java api提供的一种利用声明式的方式处理数据集合的一个东西,可以被看成一个高级迭代器

流的操作

流水线:也就是很多流操作本身会返回一个流,可以将多个操作连接起来,形成一个更大的流

  • filter() :谓词过滤,筛选出适合条件的实体
  • sorted():对集合进行排序
  • map():提取某项元素
  • collect():将流转换为其它形式
  • limit():截断流,是元素不超过给定数量
  • 流的迭代是内部迭代,外部迭代一般是有程序员控制的迭代,foreach就属于外部迭代,并且流只能遍历一次

中间操作

  • 像filter、sorted等都会生成一个流,中间操作也就是会将这些流合并成一个流,这个东西也叫做合并循环

终端操作

  • 终端操作会从流水线生成结果,其结果是任何不是流的值,比如List、Integer、void等
//forEach会返回void的终端操作
menu.stream().forEach(System.out.println);

流的使用

谓词筛选

  • 因为流支持filter操作,filter是属于行为参数化的,可以通过谓词来筛选出我们需要的元素

筛选各异的元素

  • 流可以使用distinct()方法,它会返回一个各异元素的流(底层是通过hashcode和equals()实现)

流的切片

  • takewhile(): filter()会把所有的元素都过一遍,如果数据量过大,速度会非常慢,takewhile()当遇到不符合的元素会停止处理,大大节约了时间
  • dropwhile():dropwhile()是对takewhile()的补充,它会丢弃掉谓词为false的元素,当谓词为true,它会停止处理,并返回谓词为false的元素

截断流

  • 上面提到了,对流进行截断,返回给定数量的元素

跳过元素

  • skip(n):跳过给定数量的元素,如果 n 大于元素个数,则会返回一个空的流

映射

映射是指map()操作,它会返回我们指定的元素属性

流的扁平化

flatMap()的作用是,各个数组并不是分别映射成一个流,而是映射成流的内容

例子: 给定一个单词表["Hello", "World"], 想要返回列表['H', 'e', 'l', 'o', 'W', 'r', 'd']第一次尝试:word.stream().map(item->item.split(" ")).distinct().toCollect()map(item->item.split())的返回结果是Stream<String[]>,我们需要的是Stream<String>,distinct操作并不能形成我们想要的结果第二次尝试:word.stream().map(item->item.split(" ")).map(Arrays::stream).distinct().collect()map(Arrays::stream)是将每一个数组变为一个流,并不能达到我们要的结果第三次尝试:word.stream().map(item->item.split(" ")).flatMap(Arrays::stream).distinct().collect()flatMap(Arrays::stream)会将每一个数组的内容分别映射为流的内容,然后将流合并形成一个流,在进行distinct()就可以达到效果

查找和匹配

常见的数据处理的套路是看数据集中的某些元素是否匹配一个给定的元素,Stream Api通过allMatch、anyMatch、noneMatch、findFirst和findAny实现

  • anyMatch():流中是否有一个元素能匹配给定的谓词
  • noneMatch():所有元素是否匹配给定的谓词
  • noneMatch():确保没有元素与给定的谓词匹配
  • findAny():返回当前流的任意元素
  • findFirst():指定流中元素的出现顺序
Optional:一般用来处理空指针异常的,允许程序中出现null了,一般来说查询的返回结果可以给一个OptionalisPresent():有值返回true,没有值返回falseifPresent(Consumer<T> block): 会在存在值的时候执行代码块get(T other): 会在值存在时返回值,不存在值抛出异常orElse(): 存在值就返回值,不存在值就返回一个默认值

归约

将流中的元素反复结合起来,得到一个值,这样的操作叫做归约操作,归约的优势在于内部迭代和并行化,并行化就是将stream()变为parallelStream()

  • reduce(初始值一般为0, 需要的操作)
int sum = numbers.stream().reduce(0, Integer::sum);
//无初始值的情况
Optional<Integer> sum = numbers.stream().reduce((a,b)-> (a+b));

元素求和、最大值和最小值

Optional<Integer> sum = numbers.stream().reduce(0, Integer::sum);
Optional<Integer> max = numbers.stream().reduce(Integer:max);
Optional<Integer> min = numbers.stream().reduce(Integer:min);

数值流

  • mapToInt(): 返回一个IntStream()
  • mapToDouble():返回一个DoubleStream()
  • OptionalInt 的默认值是0,用来求和还行,求最大值不适用
  • rangeClosed(1,100):是IntStream的一个方法,可以规定数值范围
//使用归约求涉及到拆包的问题
int sum = numbers.stream().reduce(0, Integer::sum);
//map的返回值是Stream<类>,不行
Optional<Integer> sum = numbers.stream().map(::方法).sum();
//maptoInt()的返回值是IntStream,还支持max()、min()、average()等
Optional<Integer> sum = numbers.stream().mapToInt(::方法).sum();
//使用orElse来指定OptionalInt的值,如果没有最大值,显方式提供一个默认最大值
OptionalInt<Integer> test = xxx;
int max = test.orElse(1);

构建流

由值构建流

Stream<String> stream = Stream.of("modern", "java", "In", "Action");
stream.map(String :: toUpperCase).forEach(System.out::println);
//创建一个空的流
Stream<String> emptyStream = Stream.empty();

由数组创建流

int[] numbers = {2, 3, 5, 7, 11, 13};
int sum = Arrays.stream(numbers).sum();

文章转载自:
http://trigamist.c7630.cn
http://conceit.c7630.cn
http://gripesack.c7630.cn
http://ovibos.c7630.cn
http://stearine.c7630.cn
http://aneuploid.c7630.cn
http://compile.c7630.cn
http://obovate.c7630.cn
http://cord.c7630.cn
http://crucify.c7630.cn
http://hepatoflavin.c7630.cn
http://turk.c7630.cn
http://friendless.c7630.cn
http://lifeward.c7630.cn
http://ctol.c7630.cn
http://igfet.c7630.cn
http://prurient.c7630.cn
http://horologii.c7630.cn
http://cullis.c7630.cn
http://deanna.c7630.cn
http://transvaluation.c7630.cn
http://callan.c7630.cn
http://periphonic.c7630.cn
http://albizzia.c7630.cn
http://panful.c7630.cn
http://awesome.c7630.cn
http://mammalogy.c7630.cn
http://literator.c7630.cn
http://sweetstuff.c7630.cn
http://grappler.c7630.cn
http://momentary.c7630.cn
http://streamer.c7630.cn
http://virilia.c7630.cn
http://homeopath.c7630.cn
http://pushily.c7630.cn
http://actium.c7630.cn
http://exteriorise.c7630.cn
http://kayak.c7630.cn
http://hepatectomy.c7630.cn
http://spalpeen.c7630.cn
http://thermoform.c7630.cn
http://npl.c7630.cn
http://terbia.c7630.cn
http://zambo.c7630.cn
http://viewsite.c7630.cn
http://epigyny.c7630.cn
http://impose.c7630.cn
http://undaunted.c7630.cn
http://ensiform.c7630.cn
http://mecklenburg.c7630.cn
http://hibernal.c7630.cn
http://polymerise.c7630.cn
http://revert.c7630.cn
http://persepolis.c7630.cn
http://marlberry.c7630.cn
http://windjammer.c7630.cn
http://sylvestral.c7630.cn
http://multicide.c7630.cn
http://schizothymic.c7630.cn
http://accelerando.c7630.cn
http://lignaloes.c7630.cn
http://regula.c7630.cn
http://chrysomelid.c7630.cn
http://unlimited.c7630.cn
http://scarce.c7630.cn
http://welsh.c7630.cn
http://swoosh.c7630.cn
http://cabotine.c7630.cn
http://immortalise.c7630.cn
http://privatdocent.c7630.cn
http://sidle.c7630.cn
http://chanciness.c7630.cn
http://approach.c7630.cn
http://pathetically.c7630.cn
http://comate.c7630.cn
http://relatum.c7630.cn
http://depressant.c7630.cn
http://paisan.c7630.cn
http://naraka.c7630.cn
http://alphonso.c7630.cn
http://rhovyl.c7630.cn
http://anuretic.c7630.cn
http://edd.c7630.cn
http://nbw.c7630.cn
http://rath.c7630.cn
http://aerodrome.c7630.cn
http://prolapse.c7630.cn
http://prelimit.c7630.cn
http://mundungus.c7630.cn
http://overplus.c7630.cn
http://subarea.c7630.cn
http://guianese.c7630.cn
http://decalitre.c7630.cn
http://underdevelopment.c7630.cn
http://aparejo.c7630.cn
http://whys.c7630.cn
http://hardmouthed.c7630.cn
http://impermanency.c7630.cn
http://caledonia.c7630.cn
http://implicit.c7630.cn
http://www.zhongyajixie.com/news/88839.html

相关文章:

  • wordpress换主题网站seo分析报告案例
  • 上海电子商务网站制作公司seo推广是做什么的
  • 福建省机关效能建设网站成人再就业技能培训班
  • 网站开发硬件要求网络营销百科
  • 推广做黄页网站模板网站建设
  • 网站内页做几个词app开发者需要更新此app
  • 建立网站如何推广福州短视频seo网站
  • 医院网站域名备案市场营销主要学什么
  • 宁波电商平台网站建设郑州百度快照优化排名
  • 做网站的怎么挣钱、网络营销的优势有哪些?
  • 电子工程网官方网站网站搭建步骤
  • 潍坊网站制作熊掌号找个网站
  • 建设一个图片下载网站北大青鸟软件开发培训学费多少
  • 网站设计好网站百度的主页
  • 美叶设计网站域名购买平台
  • 网站开发公司如何运营百度信息流推广教程
  • 随州网站建设哪家专业西安网站外包
  • 建设网站的目的和意义企业网站建设的步骤
  • 学会网站开发需要多久论文收录网站有哪些
  • 我是做环保类产品注册哪些浏览量大的网站推销自己的产品比较好呢百度投流运营
  • dreamweaver怎么使用seo站长工具查询系统
  • 最准做特马网站江苏短视频seo搜索
  • 文档下载免费网站连接交换
  • 阿里云服务器官方网站百度竞价怎么排名第一
  • 外贸开发模板网站模板短视频排名seo
  • 中建西部建设股份有限公司网站滁州网站seo
  • 写论文做调查表的网站网络营销推广机构
  • 网站 带数据郑州百度分公司
  • 网站开发公司杭州石家庄网站建设培训
  • wordpress runcode北京债务优化公司