当前位置:网站首页>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 .
边栏推荐
- @ResponseBody的作用
- Using docker to manage MySQL services under Windows
- XSS challenge (1-5) more detailed answers
- XSS challenge (6-10) more detailed answers
- [buuctf] [geek challenge 2019] secret file
- Vue returns to the previous page without refreshing the page / Vue caches the page
- PHP 2D array change key name
- How to execute a query SQL
- On simple code crawling Youdao translation_ 0's problem (to be solved)
- How does hbuilder display in columns?
猜你喜欢

Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source!
![[buuctf] [actf2020 freshman competition]include](/img/42/50439290177fdea5f431e315cac1a1.jpg)
[buuctf] [actf2020 freshman competition]include

PS dynamic drawing

Getting started with shell Basics

Learn about data kinship JSON format design from sqlflow JSON format

Jetpack compose for perfect screen fit

Introduction to the construction and development of composer private warehouse

Ctfshow getting started with the web (ThinkPHP topic)

DiceCTF - knock-knock

Lifting scanning tool
随机推荐
NoViableAltException([email protected][])
Detailed explanation of the first three passes of upload Labs
org.json.JSONObject对象转json,json新增元素,根据json的key获取值。以及list对象格式字符串转jsonArray
Fastcgi CGI shallow understanding
On simple code crawling Youdao translation_ 0's problem (to be solved)
从控制层返回到js的json数据带“\”转译符,怎么去掉
Laravel upload error
PS dynamic drawing
remote: Support for password authentication was removed on August 13, 2021. Please use a personal ac
The programming competition is coming! B station surrounding, senior members and other good gifts to you!
Cost forecast of PMP (BAC, EAC, etc)
The JSON data returned from the control layer to JS has a "\" translator. How to remove it
ot initialized – call ‘refresh’ before invoking lifecycle methods via the context: Root WebApplicati
I'd like to ask you, where can I open an account in Foshan? Is it safe to open a mobile account?
Geoffreyhinton: my 50 years of in-depth study and Research on mental skills
"Persistent diseases" that cannot be solved in IM application development
I love network security for new recruitment assessment
PHP generate images into Base64
[observation] as the intelligent industry accelerates, why should AI computing power take the lead?
"As a service", the inevitable choice of enterprise digital transformation