当前位置:网站首页>Summary of traversal methods of list, set, map, queue, deque and stack
Summary of traversal methods of list, set, map, queue, deque and stack
2022-07-26 07:53:00 【I like iced black tea】
List of articles
One 、 Traverse List
1, Use get(int) Methods through , because List The behavior of is almost identical to that of an array :List Internal storage elements are stored in order according to the sequence of elements , Each element can determine its position by index ,List The index of is the same as the array , from 0 Start
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList(" Hangzhou ", " Beijing ", " Shanghai ", " nanjing ");
for (int i=0; i<list.size(); i++) {
String s = list.get(i);
System.out.println(s);
}
}
}2、 Using Iterators iterator() Traverse ,Iterator Itself an object , But it's made up of List Instance call to iterator() Method .Iterator Object knows how to traverse a List, And different List type , Back to Iterator The objects are also different , But it always has the highest access efficiency .
boolean hasnext() Determine whether the next element E next() Returns the next element
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList(" Hangzhou ", " Beijing ", " Shanghai ", " nanjing ");
for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
String s = it.next();
System.out.println(s);
}
}
}Two 、 Traverse Set
Set yes Collection The following unordered sub interfaces
characteristic : disorder 、 No index 、 Can't repeat
1、 Use toArray(), Convert to an array , Traversing an array ;
public class Main {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
Object[ ] objects = set.toArray();
for(int i = 0; i < objects.length; i++){
System.out.println(objects[i]+"");
}
}
}2、 Using Iterators iterator() Traverse
public class Main {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
for (Iterator<String> it = set.iterator(); it.hasNext(); ) {
String s = it.next();
System.out.println(s);
}
}
}3、 Use foeach Loop through
public class Main {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
for (String s : set){
System.out.println(s);
}
}
}3、 ... and 、 Traverse Map
1、 Use foreach Loop traversal Map Example of keySet() Method Set aggregate
public class Main {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
for (String key : map.keySet()){
Integer value = map.get(key);
System.out.println(key + "=" + value);
}
}
}2、 Traversal usage foreach Loop traversal Map Object's entrySet() aggregate , It contains every key-value Mapping
public class Main {
public static void main(String[] args) {
Map<String,Integer> map= new HashMap<>();
for(Map.Entry<String,Integer> entry : map.entrySet())){
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + "=" + value);
}
}
}Four 、 Traverse Queue
1、 Use foreach Loop traversal queue
public class Main {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<String>();
for(String temp:queue){
System.out.println(temp);
}
}
}2、 Using Iterators iterator() Traverse
public class Main {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<String>();
Iterator it = queue.iterator();
while(it.hasnext()){
System.out.println(it.next);
}
}
}3、 Using the queue Queue Self contained method
boolean isEmpty() Determine whether the element in the queue is empty
E poll() Take out the team leader element and delete
public class Main {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<String>();
while(!queue.isEmpty()){
System.out.println(queue.poll());
}
}
}5、 ... and 、 Traverse Deque
1、foreach Loop traversal
public class Main {
public static void main(String[] args) {
Deque<String> deque = new LinkedList<String>();
for (String string : deque) {
System.out.println(string);
}
}
}2、 Use iterators to traverse
public class Main {
public static void main(String[] args) {
Deque<String> deque = new LinkedList<String>();
Iterator<String> it = deque.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}6、 ... and 、 Traverse stack
1、while Loop traversal
public class Main {
public static void main(String[] args) {
Stack<String> stack = new Stack<String>();
while(stack.isEmpty()) {
System.out.println(stack.pop());
}
}
}
2、foreach Loop traversal
public class Main {
public static void main(String[] args) {
Stack<String> stack = new Stack<String>();
Iterator<String> it = stack.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
边栏推荐
- Establishment and use of openstack cloud platform
- Keras learning part: obtaining the output results of neural network middle layer
- 总结软件测试岗的那些常见高频面试题
- WCF deployed on IIS
- 在线问题反馈模块实战(十四):实现在线答疑功能
- Anaconda 中安装 百度飞浆Paddle 深度学习框架 教程
- [classic thesis of recommendation system (10)] Alibaba SDM model
- Learning Efficient Convolutional Networks Through Network Slimming
- Summary of distributed related interview questions
- PXE efficient batch network installation
猜你喜欢

JMeter性能测试之使用CSV文件参数化

Parameterization of JMeter performance test using CSV file

DADNN: Multi-Scene CTR Prediction via Domain-Aware Deep Neural Network

元宇宙基础设施:WEB 3.0 chain33 优势分析

什么是消息订阅和发布?

MMOE multi-objective modeling

Logical volume management (LVM)

总结软件测试岗的那些常见高频面试题

The bigger the project is, the bigger it is. This is how I split it

系统架构&微服务
随机推荐
Ethernet switching security
Distributed system and distributed database system (Introduction)
PXE efficient batch network installation
Basic knowledge of convolutional neural network
Shardingsphere data slicing
Vscode cannot start the problem solving idea
LeetCode剑指offer专项(一)整数
Simulation of transfer function step response output of botu PLC first-order lag system (SCL)
Deep learning model deployment
table 固定特定行
Regression analysis code implementation
How to close the high-level port
Summary of distributed related interview questions
2019 ZTE touyue · model compression scheme
utils 连接池
Abnormal (2)
OVS底层实现原理
为啥谷歌的内部工具不适合你?
Anaconda 中安装 百度飞浆Paddle 深度学习框架 教程
HOT100 hash