当前位置:网站首页>List、Set、Map、Queue、Deque、Stack遍历方法
List、Set、Map、Queue、Deque、Stack遍历方法
2022-08-07 05:15:00 【m.j.y.】
1.List集合遍历
方法1:for循环:遍历方式多种,可以从前往后遍历,也可以从后往前遍历,通过下标进行访问
for(int i = 0;i<list.size;i++){
System.out.print(list.get(i));
}方法2:增强for循环:底层实现使用iterator迭代器
for(String str : list){
System.out.print(s);
}方法3:迭代器
//使用 ListIterator迭代器
ListIterator<String> it = list.listIterator(list.size()-1);//获取迭代器对象 while(it.hasNext()) { //获取后一个
System.out.println(it.next());
}
ListIterator<String> it = list.listIterator(list.size()-1);//获取迭代器对象 while(it.hasPrevious()) { //获取前一个
System.out.println(it.previous());
}ListIterator迭代器可以从指定位置向前或者向后遍历
2.Set集合遍历
1.增强for循环
for(String str : set){
System.out.print(s);
}2.迭代器
Iterator<String> it = set.Iterator();//获取迭代器对象
while(it.hasNext()) { //获取后一个
System.out.println(it.next());
}3.Map集合遍历
1.通过遍历Key,获取对应的Value
for(String key : map.keySet){
Integer value = map.get(key);
System.out.print(key+"-"+value);
}2.通过遍历Entry集合,直接获取map的键值对对象
for(Entry<String, Integer> entry:map.entrySet()) {
System.out.println(entry);
}3. 使用迭代器
Iterator<Enter<String,Integer>> it = map.entrySet.Iterator();//获取迭代器对象
while(it.hasNext()) { //获取后一个
System.out.println(it.next());
}4.Queue遍历
1.while循环:遍历结束,队列为空,因为poll()方法会删除元素
String str;
while((str = queue.poll() != null){
System.out.println(str);
}2. 迭代器
Iterator<String> it = queue.iterator();//获取迭代器对象
while(it.hasNext()) {
System.out.print(it.next());
}5.Deque遍历
1.增强for循环
for(String str : deque){
System.out.println(str)
}2.迭代器
Iterator<String> it = deque.iterator();//获取迭代器对象
while(it.hasNext()) {
System.out.print(it.next());
}3.while循环
String str;
//从对头访问
while((str = deque.pollFirst() != null){
System.out.println(str);
}
//从对尾访问
while((str = deque.pollLast() != null){
System.out.println(str);
}6.Stack遍历
1.增强for循环
for(String str : stack){
System.out.println(str)
}2.迭代器
Iterator<String> it = stack.iterator();//获取迭代器对象
while(it.hasNext()) {
System.out.print(it.next());
}3.while循环
while(!stack.isEmpty()){
System.out.println(stack.pop());
}总结:
1.上述的六种集合都可采用增强for循环,而底层实现机制使用了迭代器进行遍历操作。
2.deque双端队列可根据自身数据结构的特点从对头进行遍历,也可以从队尾进行遍历。
3.list集合:有序、允许重复,有ArrayList和LinkedList;set集合:有HashSet无序、唯一LinkedHashSet 有序、唯一和TreeSet 自动排序、唯一;map集合:KV键值对方式存储、 Key键唯一,Value允许重复、无序;queue集合:先进先出(FIFO),queue是一个接口,有LinkedList和ArrayBlockingQueue两个实现类,只能从对头删除元素、从队尾添加元素;deque集合:双端队列,可以从对头或者队尾添加或删除元素;stack集合:后进先出(LIFO)。如有不当之处还请大家多多指正,一起学习,一起进步!
边栏推荐
猜你喜欢

学习资料推荐,截至20220228

DGIOT's first lightweight IoT open source platform in China - supports multi-type solar panel remote control and multi-screen operation and maintenance

序列数据和文本的深度学习

【Unity】漂浮文字

STM32跑马灯实验(寄存器版)

SPI协议

网络安全学习:系统基础命令操作

YL70 remote I/O module, can be used to measure four-way switch signal, and has four-way relay signal output

【Collection下的单列接口list的实现类ArrayList、LinkedList、迭代器】

dgiot-dtu realizes PLC data acquisition and real-time viewing on the cloud
随机推荐
dgiot-dtu realizes PLC data acquisition and real-time viewing on the cloud
红外遥控解码
序列数据和文本的深度学习
DGIoT task scheduling
Swagger前后台集成工具
DGIOT's first lightweight IoT open source platform in China - supports multi-type solar panel remote control and multi-screen operation and maintenance
FPGA基础设计之数码管显示
外卖项目部署
char *, char **,char a[] ,char *a[]啥啥分不清楚?
cloudnote云之印前后台集成思路
信号隔离器直流电流电压变送器 分配转换模块0-10V一进二出4-20mA
ZYNQ基础知识
【继承 、父类引用: super|| 权限修饰符:private、protected、默认、public || 状态修饰符:static、final】
STM32按键输入实验
基于STC8G2K64S4单片机通过I2C硬件控制OLED屏幕
AD封装库的下载与导入
DCDC low pressure booster regulated dc high voltage 12 v24v turn - 50 v100v110v150v200v250v300v350v400v500v negative voltage output
【Unity】消息协议的设计
第11天 - 矩阵
陈杰院士:多无人系统协同中的人工智能安全探索