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

集合详解之(四)集合的遍历

文章目录

  • 🐒个人主页
  • 🏅JavaSE系列专栏
    • 📖前言:
    • 🎀ArrayList集合forEach()方法遍历
    • 🎀for循环遍历(针对List集合)
    • 🪅增强for循环(也支持Set集合)(`不能遍历删除元素,否则会报并发修改异常`)
    • 🪂迭代器遍历集合
    • 🪀Stream流的遍历

🐒个人主页

🏅JavaSE系列专栏

📖前言:

本篇博客主要以介绍集合的四种遍历方法,for循环遍历,增强for循环,迭代器,Stream流遍历

🎀ArrayList集合forEach()方法遍历

		List<Integer> arrayList=new ArrayList<>();Collections.addAll(arrayList,1,2,2,3,4,5);arrayList.forEach(e->{ System.out.println(e); });

🎀for循环遍历(针对List集合)

【List集合:(for循环)】

		List<Integer> arrayList=new ArrayList<>();List<Integer> linkedList=new LinkedList<>();Collections.addAll(arrayList,1,2,3,4,5);Collections.addAll(linkedList,1,2,3,4,5);for (int i = 0; i <arrayList.size() ; i++) {System.out.println(arrayList.get(i));}

🪅增强for循环(也支持Set集合)(不能遍历删除元素,否则会报并发修改异常

【List集合:(foreach循环)】

 		List<Integer> arrayList=new ArrayList<>();List<Integer> linkedList=new LinkedList<>();Collections.addAll(arrayList,1,2,3,4,5);Collections.addAll(linkedList,1,2,3,4,5);for (Integer integer : linkedList) {System.out.println(integer);}

【Set集合:(foreach循环)】

Set<Integer> hashSet=new HashSet<>();Set<Integer> treeSet=new TreeSet<>();Collections.addAll(hashSet,1,2,3,4,5);Collections.addAll(treeSet,1,2,3,4,5);for (Integer integer : treeSet) {System.out.println(integer);}

🪂迭代器遍历集合

 		Set<Integer> hashSet=new HashSet<>();Set<Integer> treeSet=new TreeSet<>();Collections.addAll(hashSet,1,2,3,4,5);Collections.addAll(treeSet,1,2,3,4,5);Iterator<Integer> iterator=hashSet.iterator();while (iterator.hasNext()){Integer e=iterator.next();//拿到下一个元素//经常与remove()相配合来删除元素if(e==2){iterator.remove();}}

🪀Stream流的遍历

		Set<Integer> hashSet=new HashSet<>();Set<Integer> treeSet=new TreeSet<>();Collections.addAll(hashSet,1,2,3,4,5);Collections.addAll(treeSet,1,2,3,4,5);hashSet.stream().forEach(e->{ System.out.println(e); });treeSet.stream().forEach(e->{System.out.println(e);});
http://www.lryc.cn/news/45737.html

相关文章:

  • 【I2C】通用驱动i2c-dev分析
  • 用GPT-4写代码不用翻墙了?Cursor告诉你:可以~~
  • 硬件语言Verilog HDL牛客刷题day03 时序逻辑部分
  • day31 ● 455.分发饼干 ● 376. 摆动序列 ● 53. 最大子序和
  • MobTech 秒验|本机号码一键登录会泄露隐私吗
  • 2023年供销合作社研究报告
  • 【ansible】实施任务控制
  • 49天精通Java,第11天,java接口和抽象类的异同,default关键字
  • JAVA练习99-逆波兰表达式求值
  • 恶意软件、恶意软件反杀技术以及反病毒技术的详细介绍
  • 【数据库运维】mysql备份恢复练习
  • 刷题30-对称的二叉树
  • 精选简历模板
  • 蓝桥杯嵌入式第十三届客观题解析
  • 【Redis】线程问题
  • 【算法题】2498. 青蛙过河 II
  • 【新2023Q2押题JAVA】华为OD机试 - 整理扑克牌
  • 【hello C语言】文件操作
  • OBCP第八章 OB运维、监控与异常处理-数据库监控
  • 已经提了离职,还有一周就走,公司突然把我移出企业微信,没法考勤打卡, 还要继续上班吗?...
  • Win11启用IE方法
  • 有人靠ChatGPT 狂赚200W !有人到现在,连账号都没开通......
  • 基于GD32F470的mbedtls 3DES算法测试
  • 为什么一些人很瞧不起 Java?
  • DropMAE: Masked Autoencoders with Spatial-Attention Dropout for Tracking Tasks
  • 【shell 基础(11)循环之for】带列表:空格子串、换行子串、展开、命令替换、seq;不带列表:接受参数、类C
  • 虚拟环境中创建Django项目 详细完整
  • BCSP-玄子JAVA开发之JAVA数据库编程CH-08_JDBC
  • 一位程序员将一款开源工具变成了价值75亿美元的帝国
  • tmux | 终端操作软件,解决深度学习中终端相关问题