当前位置:网站首页>Summary of traversal methods of six sets list, set, map, queue, deque and stack
Summary of traversal methods of six sets list, set, map, queue, deque and stack
2022-07-25 22:48:00 【Cool hair】
1、List aggregate
List Set is our most basic set , Common implementation classes are ArrayList, Internally, an array is used as a storage structure , So it's very similar to arrays , He is an ordered list , Indexes are the same as arrays , All from subscript 0 Start . So we're traversing List when , With this 2 Methods :
① Use for loop , coordination get(index) Methods through .( Not recommended ) example :
public class Main{
public static void main(String[] args){
List<String> list = Arrays.as("A","B","C","D","E");
for(int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
}
}② Using Iterators Itearator visit List.( recommend ) example :
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList("A", "B", "C", "D","E");
for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
System.out.println(it.next());
}
}
}③ Use for each Traverse List, Its essence is the use of iterators , The principle and ② identical , But the code is simpler .( recommend ) example :
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList("A", "B", "C", "D","E");
for (String s : list) {
System.out.println(s);
}
}
}2.Set aggregate
Set Collections are used to store collections of non repeating elements , The common implementation classes are HashSet, This set is characterized by disorder and uniqueness , For internal use HashMap As a storage structure , So there is no subscript value , So it can't be used for Loop traversal , Yes Set aggregate , We usually use for each Or iterators (Iteartor) To traverse .
① Use for each Traverse . example :
public class Main {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.add("C");
set.add("D");
for (String s : set) {
System.out.println(s);
}
}
}② Using Iterators Iterator Traverse . example :
public class Main {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.add("C");
set.add("D");
Iterator it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}These two traversal methods are essentially the same , But the first way is more concise .
3.Map aggregate
Map It's an interface , The most commonly used implementation classes are HashMap, His internal key value (key-value) The data structure of the mapping table , Can pass efficiently key Go find value, about Map Come on , To traverse the key have access to for each Loop to traverse Map Example of KeySet() Method Set aggregate , He is non repetitive key aggregate .
① Traverse Map Of key value
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put(" Zhang San ", 1);
map.put(" Li Si ", 2);
map.put(" Wang Wu ", 3);
for (String key : map.keySet()) {
Integer value = map.get(key);
System.out.println(key + " = " + value);
}
}
}② At the same time through key and value It can also be used for each 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<>();
map.put(" Zhang San ", 1);
map.put(" Li Si ", 2);
map.put(" Wang Wu ", 3);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + " = " + value);
}
}
}Map The order of traversal is not necessarily put() The order of placing !
4.Queue queue
Queue A queue is a frequently used collection , It's a linear structure , Follow FIFO (FIFO:First In First Out) Principles .
① use for each Traverse the queue . example :
public static void main(String[] args) {
Queue<String> queue = new LinkedList<String>();
queue.offer("A");
queue.offer("B");
queue.offer("C");
queue.offer("D");
for (String s : queue) {
System.out.println(s);
}
}② Using iterators (Iterator) Traverse the queue
public static void main(String[] args) {
Queue<String> queue = new LinkedList<String>();
queue.offer("A");
queue.offer("B");
queue.offer("C");
queue.offer("D");
String item = null;
while((item = queue.poll()) != null) {
System.out.println(item);
}
}5、Deque deque
Deque The characteristic of double ended queue is that both ends enter , Both ends . Elements can be added to the end of the queue , It can also be added to the head of the team . This is generally not used for each Traverse , Because use for each It will traverse from the beginning by default , And lost the characteristics of double ended queues
public static void main(String[] args) {
Deque<String> deque = new LinkedList<String>();
// The team
deque.offerFirst("A");
deque.offerFirst("E");
deque.offerLast("D");
deque.offerFirst("C");
deque.offerLast("B");
System.out.println(deque);
// Traverse ( Start at the end of the team )
String item = null;
while((item = deque.pollLast()) != null) {
System.out.println(item);
}
}6、Stack Stack
A stack is a last in, first out (LIFO) Data structure of . This is related to the queue Queue First in, first out (FIFO) As like as an apple is to an oyster .Stack There are several usage scenarios :JVM Method stack 、 Use Stack Complete hexadecimal conversion and infix expression calculation . We can often use it to do string inversion directly . example :
public static void main(String[] args) {
Stack<String> stack = new Stack<String>();
// Push
stack.push("A");
stack.push("B");
stack.push("C");
stack.push("D");
stack.push("E");
// Traverse out of stack
while(!stack.isEmpty()) {
System.out.println(stack.pop());
}
// for (String string : stack) {
// System.out.println(string);
// }
}summary : As long as it's inherited Collection<E> Interface , Both can be used. for each Loop to traverse , however for each The essence of a loop is an iterator Iteartor() Use , therefore , Ergodic set , Using Iterators Iteartor() Is often the most efficient way to traverse !
边栏推荐
- 1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
- 【集训DAY13】Backpack【动态规划】【贪心】
- [training day15] simple calculation [tree array] [mathematics]
- [training Day12] x equation [high precision] [mathematics]
- Ribbon execution logic source code analysis
- Interpretation of English terms
- XSS tool beef XSS installation and use
- PE格式: 分析IatHook并实现
- [training Day12] min ratio [DFS] [minimum spanning tree]
- Domain oriented model programming
猜你喜欢

recyclerview计算滑动距离之computeHorizontalScrollExtent-computeHorizontalScrollRange-computeHorizontalScrol

We media people must have four resource tools, each of which is very practical
![[training day15] simple calculation [tree array] [mathematics]](/img/20/a5604f666ab02f47929f80c5597f0a.png)
[training day15] simple calculation [tree array] [mathematics]
![[training Day12] tree! Tree! Tree! [greed] [minimum spanning tree]](/img/46/1c7f6abc74e11c4c2e09655aade223.png)
[training Day12] tree! Tree! Tree! [greed] [minimum spanning tree]

Google analyzes how UA can be transferred to the latest version of GA4

QT Chinese programming encounters c2001 error, prompting "there is a newline character in the constant"

Qt的TQTreeWidget控件

【集训DAY13】Out race【数学】【动态规划】

3 lexical analysis

自媒体人必备的4个资源工具,每一个都很实用
随机推荐
xss-工具-Beef-Xss安装以及使用
面试题 17.11. 单词距离 ●●
【集训DAY12】X equation 【高精度】【数学】
Today, let's talk about the if branch statement of the selection structure
Tfrecord write and read
力扣解法汇总919-完全二叉树插入器
Kibana~ the process number cannot be found after kibana is started in the background
单元测试,写起来到底有多痛?
【集训DAY15】油漆道路【最小生成树】
汇编语言与微机原理实验一、实验二、实验三:分支程序设计/循环程序设计/子程序设计
JVM内存区域
[MySQL rights] UDF rights (with Malaysia)
贴片微型滚珠振动开关的结构原理
冯诺依曼体系结构
Can generic types be used in array
编译器引论
[PMP learning notes] Chapter 1 Introduction to PMP System
【自然语言处理】【向量表示】AugSBERT:改善用于成对句子评分任务的Bi-Encoders的数据增强方法
2020-09-17
Binder原理