当前位置:网站首页>六种集合的遍历方式总结(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
边栏推荐
- Abstract classes and interfaces
- String class
- 阿里云微服务(三)Sentinel开源流控熔断降级组件
- 分支语句和循环语句
- Tyut Taiyuan University of technology 2022 introduction to software engineering examination question outline
- Experience summary of autumn recruitment of state-owned enterprises
- 阿里云一面:并发场景下的底层细节 - 伪共享问题
- 一文搞定 UDP 和 TCP 高频面试题!
- 记录:动态Web项目servlet访问数据库404错误之解决
- Fgui project packaging and Publishing & importing unity & the way to display the UI
猜你喜欢
TYUT太原理工大学2022数据库大题之E-R图转关系模式
One article to get UDP and TCP high-frequency interview questions!
10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
121道分布式面试题和答案
TYUT太原理工大学2022软工导论大题汇总
Inheritance and polymorphism (I)
系统设计学习(一)Design Pastebin.com (or Bit.ly)
Alibaba cloud microservices (III) sentinel open source flow control fuse degradation component
String类
随机推荐
TYUT太原理工大学2022软工导论简答题
Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology
TYUT太原理工大学往年数据库简述题
String class
阿里云微服务(四) Service Mesh综述以及实例Istio
Data manipulation language (DML)
Quickly generate illustrations
系统设计学习(一)Design Pastebin.com (or Bit.ly)
Small exercise of library management system
162. Find peak - binary search
System design learning (I) design pastebin com (or Bit.ly)
How to ensure data consistency between MySQL and redis?
Chromatic judgement bipartite graph
4.30 dynamic memory allocation notes
Alibaba cloud microservices (IV) service mesh overview and instance istio
Inheritance and polymorphism (Part 2)
Music playback (toggle & playerprefs)
[rtklib 2.4.3 B34] version update introduction I
十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩
记录:下一不小心写了个递归