0

I am new to Java learning the collection topics. Can anyone let me know why the output varies when push method is used in ArrayDeque and Stack ?

ArrayDeque sample program :

public class Simple4 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

ArrayDeque q = new ArrayDeque();

q.push("e");
q.push("f");

System.out.println(q);
}
}

Output : [f, e]

Stack Sample program:

public class Simple5 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

Stack s = new Stack();
s.push("apple");
s.push("banana");


System.out.println(s);
}
}

Output : [apple, banana]

2
  • ordering of ArrayDeque iterator(accessed by it's toString() method is from head to tail(LIFO), you get the same order when you use pop() Commented May 27, 2018 at 17:10
  • In general, if you are asking about why two code snippets behave differently, it is a good idea to supply the same inputs. In this case, add the same list elements, in order to make the differences directly comparable. Commented May 27, 2018 at 17:16

2 Answers 2

2

Your examples are both raw-types. Don't use raw-types. Also, your Stack is using the default output format of a Collection - notice that elements are coming in FIFO order (not LIFO). To correctly consume a Stack, use pop(). Like,

Stack<String> s = new Stack<>();
s.push("apple");
s.push("banana");
while (!s.isEmpty()) {
    System.out.println(s.pop());
}

Which outputs (in LIFO) like

banana
apple
Sign up to request clarification or add additional context in comments.

Comments

0

Don't be fooled into thinking that Stack.push and ArrayDeque.push are in any way related. Despite the same name and parameters, they have nothing to do with one another.

Stack.push is defined on the Stack class.

ArrayDeque.push implements the Deque.push method. But a Stack isn't a Deque - you only put things on and take things off one end, whereas a sequence allows you to put and take at both ends (the "De" means "double ended").

Since they are unrelated methods on unrelated classes, you shouldn't expect them to do the same thing. They might, of course - you need to check the Javadoc to see what the contracts of the classes are - there should simply be no expectation that they do.

3 Comments

No explanation as to which does what? You should probably explain what the difference is.
@mackycheese21 no, my point in the last paragraph: when you use different classes, they do different things. You need (/one needs) to read the Javadoc to understand the difference.
One might think it would be helpful to note the Stack Javadoc explicitly says A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.