Page 1 of 1

About Question enthuware.ocpjp.v7.2.1148 :

Posted: Fri Dec 12, 2014 3:06 am
by rocky_bgta
what is difference between peek(), peekFirst() method of Deque

Re: About Question enthuware.ocpjp.v7.2.1148 :

Posted: Fri Dec 12, 2014 5:24 am
by admin
No difference. peek belongs to Queue interface (which Deque implements) while peekFirst is specific Deque.
You might want to go through this: https://docs.oracle.com/javase/7/docs/a ... .html#peek()

Re: About Question enthuware.ocpjp.v7.2.1148 :

Posted: Sun Feb 11, 2018 8:30 pm
by thodoris.bais
Wait, if peek() returns, but does not remove, I expect its behavior similar to a printout.
That said, I expect the element that is picked to be still present in the Deque, since it is not removed at all.

The behavior, though, is more like removing it.

Re: About Question enthuware.ocpjp.v7.2.1148 :

Posted: Sun Feb 11, 2018 9:48 pm
by admin
Did you read the API description mentioned above? Neither of the methods remove the element. They work exactly the same.

Re: About Question enthuware.ocpjp.v7.2.1148 :

Posted: Mon Feb 12, 2018 5:59 pm
by thodoris.bais
Of course:
Retrieves, but does not remove
By the above, I understand that it indeed will return 3, but will not remove it from the queue, which implies that the Dequeue structure will still contain 2,1,3 after d.peekFirst() is executed.

However, the method (peekFirst()) that is supposed to not remove the head, is actually removing it from the collection.

What am I missing?

Re: About Question enthuware.ocpjp.v7.2.1148 :

Posted: Mon Feb 12, 2018 10:38 pm
by admin
Really sorry but I am still not clear about what you are saying. Neither peek() nor peekFirst() remove anything from anywhere. They just return the value but do not remove. They work exactly the same. There is no difference as the JavaDoc for peek method of Deque clearly says, "This method is equivalent to peekFirst()."

Re: About Question enthuware.ocpjp.v7.2.1148 :

Posted: Thu Sep 22, 2022 9:59 pm
by cjgiron
Hi Admin,

This line in the first option explanation confuses me:
push() is a stack method that adds the element to the front.
In a regular Stack, the push() method adds the element to the end of the Stack. Is the push() method overridden in the Deque class to do the opposite?

Re: About Question enthuware.ocpjp.v7.2.1148 :

Posted: Fri Sep 23, 2022 12:38 am
by admin
No, in a regular stack, push adds the element to the "top" on the stack. The words "start" and "end" are meaningless for a stack. They make sense in the queue world.

This page has comparison of all of the important methods of Stack, Queue, and Deque. You may go through it for details of all the method as well: https://docs.oracle.com/en/java/javase/ ... Deque.html

Re: About Question enthuware.ocpjp.v7.2.1148 :

Posted: Fri Sep 23, 2022 9:12 pm
by cjgiron
Ohh I think I see now. So push always adds to the "top", its just because Stack is LIFO and Deque is FIFO that "top" refers to opposite insert sites?

Re: About Question enthuware.ocpjp.v7.2.1148 :

Posted: Sat Sep 24, 2022 12:27 am
by admin
Right. See this code:

Code: Select all

        Deque<Integer> d = new ArrayDeque<>();
        d.add(1);
        d.add(2);
        //d.push(3); 
        System.out.println(d.remove()); 
It prints 1 (and not 2) because add/remove are queue methods and since queue is FIFO, d.add adds the element to the tail/end and d.remove removes an element from the head/start.

But if you uncomment d.push(3), you are using a stack method that adds an element to the "top" of the stack. Top of a stack corresponds to the head of a queue), which means, 3 goes to the head and so d.remove will now print 3 instead of 1.