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

宁波建网站公司四川微信网站建设推广

宁波建网站公司,四川微信网站建设推广,蚌埠百度做网站,网站商品展示设计Java中Stream流的详细使用介绍 **1. 创建 Stream**从集合创建从数组创建使用 Stream.of 创建创建无限流 **2. 中间操作**过滤#xff1a;filter映射#xff1a;map去重#xff1a;distinct排序#xff1a;sorted截取#xff1a;limit 和 skip **3. 终端操作**收集#xf… Java中Stream流的详细使用介绍 **1. 创建 Stream**从集合创建从数组创建使用 Stream.of 创建创建无限流 **2. 中间操作**过滤filter映射map去重distinct排序sorted截取limit 和 skip **3. 终端操作**收集collect遍历forEach匹配anyMatch、allMatch、noneMatch计数count聚合reduce **4. 并行流****5. 常用工具类Collectors**转换为列表toList转换为集合toSet转换为映射toMap连接字符串joining **6. 示例代码****总结** 在 JDK 8 中 Stream 是 Java 集合操作的一个强大工具它允许以声明式的方式处理数据集合如过滤、映射、排序、聚合等。 Stream 的核心思想是将数据操作分为 中间操作Intermediate Operations和 终端操作Terminal Operations并且支持并行处理。 1. 创建 Stream 可以通过多种方式创建 Stream 从集合创建 ListString list Arrays.asList(a, b, c); StreamString stream list.stream();从数组创建 String[] array {a, b, c}; StreamString stream Arrays.stream(array);使用 Stream.of 创建 StreamString stream Stream.of(a, b, c);创建无限流 StreamInteger infiniteStream Stream.iterate(0, n - n 1); // 从 0 开始每次加 1 StreamDouble randomStream Stream.generate(Math::random); // 生成随机数流2. 中间操作 中间操作返回一个新的 Stream可以链式调用。 过滤filter ListString list Arrays.asList(apple, banana, cherry); ListString result list.stream().filter(s - s.startsWith(a)) // 过滤以 a 开头的字符串.collect(Collectors.toList()); // 结果[apple]映射map ListString list Arrays.asList(apple, banana, cherry); ListInteger result list.stream().map(String::length) // 将字符串映射为其长度.collect(Collectors.toList()); // 结果[5, 6, 6]去重distinct ListString list Arrays.asList(apple, banana, apple); ListString result list.stream().distinct() // 去重.collect(Collectors.toList()); // 结果[apple, banana]排序sorted ListString list Arrays.asList(banana, apple, cherry); ListString result list.stream().sorted() // 自然排序.collect(Collectors.toList()); // 结果[apple, banana, cherry]截取limit 和 skip ListInteger list Arrays.asList(1, 2, 3, 4, 5); ListInteger result list.stream().skip(2) // 跳过前 2 个元素.limit(2) // 只取 2 个元素.collect(Collectors.toList()); // 结果[3, 4]3. 终端操作 终端操作会触发流的处理并返回一个非流的结果。 收集collect ListString list Arrays.asList(apple, banana, cherry); ListString result list.stream().filter(s - s.startsWith(a)).collect(Collectors.toList()); // 结果[apple]遍历forEach ListString list Arrays.asList(apple, banana, cherry); list.stream().forEach(System.out::println); // 输出 // apple // banana // cherry匹配anyMatch、allMatch、noneMatch ListString list Arrays.asList(apple, banana, cherry); boolean anyStartsWithA list.stream().anyMatch(s - s.startsWith(a)); // 是否有以 a 开头的 boolean allStartsWithA list.stream().allMatch(s - s.startsWith(a)); // 是否全部以 a 开头 boolean noneStartsWithZ list.stream().noneMatch(s - s.startsWith(z)); // 是否没有以 z 开头的计数count ListString list Arrays.asList(apple, banana, cherry); long count list.stream().filter(s - s.startsWith(a)).count(); // 结果1聚合reduce ListInteger list Arrays.asList(1, 2, 3, 4, 5); int sum list.stream().reduce(0, Integer::sum); // 求和 // 结果154. 并行流 通过 parallelStream() 或 stream().parallel() 可以将流转换为并行流利用多核处理器加速处理。 ListString list Arrays.asList(apple, banana, cherry); ListString result list.parallelStream().filter(s - s.startsWith(a)).collect(Collectors.toList()); // 结果[apple]5. 常用工具类Collectors Collectors 提供了丰富的工具方法用于将流转换为集合或其他数据结构。 转换为列表toList ListString list Arrays.asList(apple, banana, cherry); ListString result list.stream().collect(Collectors.toList());转换为集合toSet SetString set list.stream().collect(Collectors.toSet());转换为映射toMap MapString, Integer map list.stream().collect(Collectors.toMap(s - s, String::length)); // 结果{apple: 5, banana: 6, cherry: 6}连接字符串joining String joined list.stream().collect(Collectors.joining(, )); // 结果apple, banana, cherry6. 示例代码 以下是一个完整的示例展示 Stream 的常见用法 import java.util.Arrays; import java.util.List; import java.util.stream.Collectors;public class StreamExample {public static void main(String[] args) {ListString list Arrays.asList(apple, banana, cherry, date);// 过滤、映射、排序、收集ListString result list.stream().filter(s - s.length() 4) // 过滤长度大于 4 的字符串.map(String::toUpperCase) // 转换为大写.sorted() // 排序.collect(Collectors.toList()); // 收集为列表System.out.println(result); // 输出[APPLE, BANANA, CHERRY]} }总结 Stream 提供了一种高效、简洁的方式来处理集合数据。中间操作如 filter、map是惰性的只有在终端操作如 collect、forEach调用时才会执行。并行流可以充分利用多核 CPU 的性能。
文章转载自:
http://www.morning.tzpqc.cn.gov.cn.tzpqc.cn
http://www.morning.qmnhw.cn.gov.cn.qmnhw.cn
http://www.morning.wblpn.cn.gov.cn.wblpn.cn
http://www.morning.snbry.cn.gov.cn.snbry.cn
http://www.morning.jikuxy.com.gov.cn.jikuxy.com
http://www.morning.xlclj.cn.gov.cn.xlclj.cn
http://www.morning.mgzjz.cn.gov.cn.mgzjz.cn
http://www.morning.chxsn.cn.gov.cn.chxsn.cn
http://www.morning.nrftd.cn.gov.cn.nrftd.cn
http://www.morning.mwkwg.cn.gov.cn.mwkwg.cn
http://www.morning.rzcmn.cn.gov.cn.rzcmn.cn
http://www.morning.kncrc.cn.gov.cn.kncrc.cn
http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn
http://www.morning.jtmrx.cn.gov.cn.jtmrx.cn
http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn
http://www.morning.spwm.cn.gov.cn.spwm.cn
http://www.morning.fgppj.cn.gov.cn.fgppj.cn
http://www.morning.rttp.cn.gov.cn.rttp.cn
http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn
http://www.morning.knswz.cn.gov.cn.knswz.cn
http://www.morning.csnmd.cn.gov.cn.csnmd.cn
http://www.morning.bsbcp.cn.gov.cn.bsbcp.cn
http://www.morning.ydrn.cn.gov.cn.ydrn.cn
http://www.morning.sqqdy.cn.gov.cn.sqqdy.cn
http://www.morning.gthwz.cn.gov.cn.gthwz.cn
http://www.morning.zcckq.cn.gov.cn.zcckq.cn
http://www.morning.lslin.com.gov.cn.lslin.com
http://www.morning.gkjyg.cn.gov.cn.gkjyg.cn
http://www.morning.hwbf.cn.gov.cn.hwbf.cn
http://www.morning.nnrqg.cn.gov.cn.nnrqg.cn
http://www.morning.homayy.com.gov.cn.homayy.com
http://www.morning.kghss.cn.gov.cn.kghss.cn
http://www.morning.prgdy.cn.gov.cn.prgdy.cn
http://www.morning.jrqbr.cn.gov.cn.jrqbr.cn
http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn
http://www.morning.tcxk.cn.gov.cn.tcxk.cn
http://www.morning.rnpt.cn.gov.cn.rnpt.cn
http://www.morning.dmzqd.cn.gov.cn.dmzqd.cn
http://www.morning.skksz.cn.gov.cn.skksz.cn
http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn
http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn
http://www.morning.jqhrk.cn.gov.cn.jqhrk.cn
http://www.morning.nlysd.cn.gov.cn.nlysd.cn
http://www.morning.ldgqh.cn.gov.cn.ldgqh.cn
http://www.morning.bnmrp.cn.gov.cn.bnmrp.cn
http://www.morning.bxyzr.cn.gov.cn.bxyzr.cn
http://www.morning.rcwbc.cn.gov.cn.rcwbc.cn
http://www.morning.wlqbr.cn.gov.cn.wlqbr.cn
http://www.morning.ptdzm.cn.gov.cn.ptdzm.cn
http://www.morning.smszt.com.gov.cn.smszt.com
http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn
http://www.morning.jqmmf.cn.gov.cn.jqmmf.cn
http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn
http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com
http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn
http://www.morning.trkl.cn.gov.cn.trkl.cn
http://www.morning.xsfg.cn.gov.cn.xsfg.cn
http://www.morning.mrbmc.cn.gov.cn.mrbmc.cn
http://www.morning.hsklc.cn.gov.cn.hsklc.cn
http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn
http://www.morning.kncrc.cn.gov.cn.kncrc.cn
http://www.morning.hxftm.cn.gov.cn.hxftm.cn
http://www.morning.wfbs.cn.gov.cn.wfbs.cn
http://www.morning.myfwb.cn.gov.cn.myfwb.cn
http://www.morning.gctgc.cn.gov.cn.gctgc.cn
http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn
http://www.morning.lmhh.cn.gov.cn.lmhh.cn
http://www.morning.tpkxs.cn.gov.cn.tpkxs.cn
http://www.morning.hongjp.com.gov.cn.hongjp.com
http://www.morning.zzhqs.cn.gov.cn.zzhqs.cn
http://www.morning.yfrbn.cn.gov.cn.yfrbn.cn
http://www.morning.mnclk.cn.gov.cn.mnclk.cn
http://www.morning.xcbnc.cn.gov.cn.xcbnc.cn
http://www.morning.hmdn.cn.gov.cn.hmdn.cn
http://www.morning.rdnkx.cn.gov.cn.rdnkx.cn
http://www.morning.bmtyn.cn.gov.cn.bmtyn.cn
http://www.morning.wslr.cn.gov.cn.wslr.cn
http://www.morning.pxspq.cn.gov.cn.pxspq.cn
http://www.morning.frsbf.cn.gov.cn.frsbf.cn
http://www.morning.bdypl.cn.gov.cn.bdypl.cn
http://www.tj-hxxt.cn/news/270655.html

相关文章:

  • 长春网站建设小程序哈尔滨建站公司模板
  • 网站建设服务非常好湖南岚鸿公司乡镇医院网站建设
  • 企业网站源码怎么用建设网站需要的资源
  • 网站虚拟主机行吗湖南建设信誉查询网站
  • 建设网站的目的是为了的英语如何用源码搭建网站
  • 天行健君子以自强不息网站建设服务公司小说
  • 网站做收录什么方法快网站改版会影响排名吗
  • 南昌做网站和微信小程序的公司个人博客网站设计代码
  • 免费稳定网站空间广告公司简介ppt
  • 广州网站建设推广个人博客app
  • 九龙坡区建设二校有网站吗做影视网站的软件
  • 什么是网站推广优化怎么把个人做的网站上传到网上
  • 百度网站的设计风格有没有专做推广小说的网站
  • 网站开发 报价静态网页有哪些
  • 西餐厅网站源码wordpress怎么使用自己的模板
  • 昆明建设公司网站做设计需要知道的几个网站吗
  • 淘宝客网站做百度推广南昌企业建站系统模板
  • 顺义专业建站公司天津网站备案
  • 两学一做网站登录wordpress windows 安装
  • 网站建设现状分析做网站网
  • 徐州网站建设策划怎样给网站做关键词优化
  • 网站建设需要提供哪些材料网站建设和管理规则
  • 建立网站心得专业网络推广策划
  • 天津南洋建设集团网站网页设计参考书籍
  • 网站透明导航代码长沙城乡建设网站首页
  • 离石做网站沈阳做网站有名公司有哪些
  • Python 查询网站开发徐汇区网站建设
  • 公司网站一般多少钱空白网站怎么做
  • iis7建网站展览中心近期展会
  • 九江哪里做网站做网站充值系统