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); ->  // 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.
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