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

Kendo UI datasource transport read ignore data parameters
Why does this recursive method keeps calling itself even though the call is inside if statement?
java having trouble with multiple classes
Evaluate Infix arithmetic expression using two stacks
No enclosing instance of the type TObjectHash<T> is accessible in scope. Spigot plugin
What is the equivalent of Oracle's ANYINTERACT+TOUCH spatial operator for Vividsolution's Geometry class?
Calling wait() nulls a variable i've just written
Running Selenium Tests using TestNG in parallel causes errors
How to use criteria query builders with dynamic constructor in select?
Compare the ArrayList values and HashMap values
Android: Error when i try to add view while “setContentView()” not specified
Handle click for layout inside preference fragment
How to make a feature to share content in social media?
Spring data elasticsearch GeoPoint with spring mvc
Exception handling request to /customer/upload/: org.jboss.resteasy.spi.UnhandledException: java.lang.VerifyError: Bad type on operand stack
Pixelating a Picture's remaining pixels

Categories

HOME
arduino-uno
client
smarty
include
cplex
programming-languages
baqend
windows-10-universal
volttron
resize
worldwind
reactive-cocoa
database-replication
pythonanywhere
crystal-reports-2008
plunker
swiftlint
object-detection
excel-2007
selectedindexchanged
user-interaction
phpfox
typo3-6.2.x
neo4j-spatial
dbclient
atlassian-crucible
webkitspeechrecognition
gitignore
google-cloud-nl
procdump
binary-data
key-value-observing
exuberant-ctags
wpf-controls
jquery-bootgrid
withings
starteam
streamreader
angularjs-factory
android-tabhost
elmah
android-mediaprojection
businessworks
qwt
logfiles
angular-resource
flickr-api
rainbowtable
network-flow
react-scripts
hendrix
query-performance
messenger
visual-c++-2008
gridpane
prettytensor
design-by-contract
oauth2client
teamcity-8.0
thredds
wordml
libressl
sankey-diagram
census
directoryservices
code-access-security
castle
sgen
uitouch
wp-query
floating-point-precision
access-rights
jquery-mobile-dialog
sqlperformance
kyotocabinet
browser-detection
dmoz
hamiltonian-cycle
runas
word-processor
parametric-equations
qtkit
spring-portlet-mvc
whoosh
libc++
xmlspy
django-tagging
invite
javax.script
servicehost
createwindow
web-application-design
preference
brewmp

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App