当前位置:网站首页>六种集合的遍历方式总结(List Set Map Queue Deque Stack)
六种集合的遍历方式总结(List Set Map Queue Deque Stack)
2022-07-06 09:19:00 【[email protected]】
List
方式一:for循环
for(int i = 0;i = list.size();i++){
System.out.println(list.get(i));
}
方式二:for each循环遍历
for(String s : list){
System.out.println(s);
}
方式三:iterator迭代器遍历
Iterator<String> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
Set
方式一:for each
for(String s : set){
System.out.println(s);
}
方式二:iterator迭代器遍历
Iterator it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
Map
方式一:for each 遍历Map的key值
for(String key : map.keySet()){
Integer value = map.get(key);
System.out.println(key + "=' =value);
}
方式二:for each 同时遍历key和value
for(Map.Entry<String,Integer> entry : map.entrySet()){
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + "=" + value):
}
Queue队列(FIFO)
方式一: for each 遍历队列
for(String s : queue){
System.out.println(s);
}
方式二:iterator 迭代器遍历队列
String item = null;
while((item = queue.poll()) != null){
System.out.println(item);
}
Deque双端队列
iterator迭代器遍历
String item = null;
while((item = deque.poll.set()) != null){
System.out.println(item);
}
Stack栈(LIFO)
while(!stack.isEmpty()){
System.out.println(stack.poll());
}
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_49194330/article/details/124734835
边栏推荐
- 一文搞定 UDP 和 TCP 高频面试题!
- Abstract classes and interfaces
- Share a website to improve your Aesthetics
- 系统设计学习(一)Design Pastebin.com (or Bit.ly)
- Chromatic judgement bipartite graph
- 十分鐘徹底掌握緩存擊穿、緩存穿透、緩存雪崩
- 记录:下一不小心写了个递归
- Tyut Taiyuan University of technology 2022 introduction to software engineering examination question outline
- [Topic terminator]
- A brief introduction to the database of tyut Taiyuan University of technology in previous years
猜你喜欢
What are the advantages of using SQL in Excel VBA
The port is occupied because the service is not shut down normally
Data manipulation language (DML)
2022 National Games RE1 baby_ tree
View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
阿里云微服务(四) Service Mesh综述以及实例Istio
2年经验总结,告诉你如何做好项目管理
TYUT太原理工大学2022软工导论大题汇总
121道分布式面试题和答案
One article to get UDP and TCP high-frequency interview questions!
随机推荐
TYUT太原理工大学2022软工导论考试题型大纲
[while your roommate plays games, let's see a problem]
分支语句和循环语句
系统设计学习(一)Design Pastebin.com (or Bit.ly)
记录:Navicat Premium初次无法连接数据库MySQL之解决
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
[GNSS] robust estimation (robust estimation) principle and program implementation
TYUT太原理工大学2022数据库大题之E-R图转关系模式
Music playback (toggle & playerprefs)
Basic DOS commands
MySQL backup -- common errors in xtrabackup backup
错误: 找不到符号
Employment of cashier [differential constraint]
记录:动态Web项目servlet访问数据库404错误之解决
记录:newInstance()过时的代替方法
First acquaintance with C language (Part 1)
Record: solution of 404 error of servlet accessing database in dynamic web project
Sharing ideas of on-chip transplantation based on rtklib source code
Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology
Tyut Taiyuan University of technology 2022 introduction to software engineering examination question outline