java


What happens when using push(), offer() and add() methods in ArrayDeque at the same time?


ArrayDeque has methods for stacks and queues. Mostly used stack and queue methods are the ones in the below:
Stack methods: push/poll/peek
Queue methods: push/poll/peek
The thing that I've done in the below code block is that, I tried to understand behavior of the ArrayDeque when both offer, push and add methods are used in the same object. The code that I wrote and its output is given below. What is the behavior of the ArrayDeque after calling push() method, which it assumes itself as a stack, and then calling the offer() method, which is stated as queue methods.
Deque<Integer> arrayDeque = new ArrayDeque<>();
arrayDeque.add(3);
arrayDeque.push(4);
arrayDeque.offer(6);
arrayDeque.addFirst(2);
arrayDeque.addLast(5);
arrayDeque.addFirst(1);
System.out.println("ArrayDeque: " + arrayDeque.toString());
The output is:
ArrayDeque: [1, 2, 4, 3, 6, 5]
Here is what it does step by step
// Add 3 at the tail of this deque
arrayDeque.add(3); -> [3]
// Add 4 at the head of this deque
arrayDeque.push(4); -> [4, 3]
// Add 6 at the tail of this deque
arrayDeque.offer(6); -> [4, 3, 6]
// Add 2 at the head of this deque
arrayDeque.addFirst(2); -> [2, 4, 3, 6]
// Add 5 at the tail of this deque
arrayDeque.addLast(5); -> [2, 4, 3, 6, 5]
// Add 1 at the head of this deque
arrayDeque.addFirst(1); -> [1, 2, 4, 3, 6, 5]
Keep in mind that the main purpose of a Deque unlike a Queue or a Stack is to have the ability to access/add the elements at/to both ends (head and tail).
1.offer-This method inserts the specified element at the end of this deque.
2.add-This method inserts the specified element at the end of this deque.
3.push-This method pushes an element onto the stack represented by this deque.
4.addFirst-This method inserts the specified element at the front of this deque.
5.addLast-This method inserts the specified element at the end of this deque.
What don't you understand?
Could you explain the behaviour of the ArrayDeque after calling push() method, which it assumes itself as a stack, and then calling the offer() method, which is stated as queue methods in the JavaDoc
Take a look at the Javadoc : https://docs.oracle.com/javase/7/docs/api/java/util/ArrayDeque.html
push method insert at the beginning and offer insert at the end.

Related Links

Hibernate not making insertions
Android: Tesseract OCR not recognizing digits
Selenium webdriver find texy by first digits
How to add auto increment coloumn in Oracle and access with java?
why is it not possible to throw checked Exception in java 8 forEach with lambda [duplicate]
If Java is Strongly typed then why does this code compile? [closed]
Unauthorized error while trying to intercept Retrofit request
How to add OJDBC6.jar in build.gradle file?
where I need to add “wait.ignoring” to “ExpectedConditions.visibilityOfElementLocated”
Most efficient way to retrieve key from value in a Hashmap without using BiMap
Get string from #Test annotation TestNG
Check for wrapped elements in JavaFX-FlowPane
Java ProcessBuilder: stream closed
Java lists and class inheritance
Issue with activities?
Generic error: “Error uploading to the iTunes Store” in submission

Categories

HOME
testing
iot
adb
getelementsbytagname
azure-media-services
multiple-records
gitpitch
windows-10-universal
kentor-authservices
dax
floating-action-button
orchardcms
pc
reactive-cocoa
evopdf
conemu
ehcache
dcevm
realex-payments-api
bootstrap-tour
status
chromebook
web-api-testing
tinymce-4
frame
phpfreechat
picasso
wixsharp
suricata
scaffold
retina-display
hue
particles.js
core-plot
botbuilder
galleria
long-polling
smartcontracts
react-scripts
jquery-nestable
promela
boost-multi-index
pebble-watch
powercli
android-cursor
mako
minimization
blacklist
or-tools
messenger
rvest
simple-framework
wordml
citrus-pay
android-listview
processmodel
device-orientation
nsmutabledictionary
modalpopup
gadt
android-imagebutton
xojo
funcunit
mechanize-ruby
terminal-services
quantlib-swig
jmapviewer
bulkloader
sqlperformance
ember-app-kit
free-variable
propertyeditor
dsn
regsvr32
wse3.0
bigcouch
zpt
infobox
gdlib
yslow
dentrix
modelstate
routedevent
nhibernate.search
forums
ti-dsp
avatar
sustainable-pace
caching-application-block

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App