当前位置:网站首页>August 24, 2021 deque queue and stack
August 24, 2021 deque queue and stack
2022-06-30 14:33:00 【tutou_ girl】

Deque It's the realization of queue Interface's double ended queue
Deque Usage scenarios of
In general , When concurrency is not involved , There are two implementation classes , It can be selected according to its own characteristics , Namely :
- LinkedList Variable size linked list double ended queue , Allow elements to be inserted null.
- ArrayDeque Variable size array double ended queue , Inserting... Is not allowed null.
- ConcurrentLinkedDeque Variable size and thread safe linked list double ended queue , Non blocking , Inserting... Is not allowed null.
- LinkedBlockingDeque Thread safe double ended queues , When the queue is empty , Get operation will block , Until an element is added .
Be careful :LinkedList and ArrayDeque Is a thread unsafe container .
Java There is Stack This class , But it is not recommended . Usually use Deque To complete the functions of queue and stack .
Deque It's an interface , In fact, there are :
- ArrayDeque, Use “ Array ” Store the data
- LinkedList, Use “ Linked list ” Store the data
- ConcurrentLinkedDeque, Thread safe LinkedList
The queue is a typical fifo (FIFO) The container of . That is to put things from one end of the container , Take out... From the other end , And the order in which things are put into the container is the same as the order in which they are taken out .
Two ways to implement queues :

1: Deque<String> queue = new LinkedList<String>();
2: queue.offer("data1"); // Add an element at the end of the queue
3: queue.offer("data2");
4: queue.offer("data3");
5: System.out.println(queue.poll()); // Get the first element of the team , And delete from the queue
6:
7: Deque<String> stack = new LinkedList<String>();
8: stack.push("element1"); // Push the elements towards the top of the stack
9: stack.push("element2");
10: stack.push("element3");
11: System.out.println(stack.pop()); // Get the top of the stack element , And delete from the top of the stack
1、offer() and add() The difference between ------------------ The team
add() and offer() It's all about adding an element to the queue . But if you want to add a new element to a full queue , call add() Method will throw a unchecked abnormal , And call offer() Method will return false. According to this, we can make an effective judgment in the program !
2、peek() and element() The difference between ----------------------- Take the team leader
peek() and element() Will return to the head of the team without removing it , however peek() Method returns... When the queue is empty null, call element() Method will throw NoSuchElementException abnormal .
3、poll() and remove() The difference between ------------------------ Out of the team
poll() and remove() All will be removed and returned to the opposite , But in poll() Returns... When the queue is empty null, and remove() Will throw out NoSuchElementException abnormal .
边栏推荐
- V3 01_ Welcome
- For loop and promise to solve the problem of concurrent callback
- jsPlumb. Deleteeveryconnection is not a function & jsplumb clear canvas jsplumb delete all nodes and all connections
- Hbuilder most commonly used and full shortcut key set
- Error on datetime when importing SQL file from MySQL
- C language & the difference between the address pointed to and the address pointed to by the pointer
- @ResponseBody的作用
- Go common lock mutex and rwmutex
- remote: Support for password authentication was removed on August 13, 2021. Please use a personal ac
- PHP recursive multi-level classification, infinite classification
猜你喜欢

Shell programming overview

Realize a simple LAN communication (similar to feiqiu)

DefCamp Capture the Flag (D-CTF) 2021-22 web

On simple code crawling Youdao translation_ 0's problem (to be solved)

Not satisfied with markdown native code block style? Try this beautify code screenshot tool~~

Rpm2rpm packaging steps

The first three passes of sqli Labs

Att & CK red team evaluation field (I)

ctfshow nodejs

ThinkPHP v3.2 comment annotation injection write shell
随机推荐
V3 03_ Getting started
Getting started with shell Basics
Thoughts on the security of a PHP file name regular verification
Laravel configures passport and returns token using JWT
Chapter 13 signal (III) - example demonstration
Detailed explanation of the first three passes of upload Labs
【BUUCTF】 EasySql
Learn about data kinship JSON format design from sqlflow JSON format
Use PHP to delete the specified text content in the file
[scientific research data processing] [practice] frequency analysis chart of category variables, distribution chart of numerical variables and normality test (including lognormal)
When SQL queries are performed in table storage, an error is reported when the primary key is added to the query result, and the query result exceeds 10W rows. Do you want to add multiple indexes to t
Lfi-rce without controllable documents
Small exercise of process and signal
Vue returns to the previous page without refreshing the page / Vue caches the page
Data recovery software easyrecovery15 Download
数据恢复软件EasyRecovery15下载
Upgrade composer self update
@ResponseBody的作用
JS array sorting method summary
Notes on reverse learning in the first week of winter vacation