About Question enthuware.ocpjp.v7.2.1148 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
rocky_bgta
Posts: 12
Joined: Thu Dec 11, 2014 12:32 am
Contact:

About Question enthuware.ocpjp.v7.2.1148 :

Post by rocky_bgta »

what is difference between peek(), peekFirst() method of Deque

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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()
If you like our products and services, please help us by posting your review here.

thodoris.bais
Posts: 25
Joined: Sat Jun 03, 2017 4:56 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Did you read the API description mentioned above? Neither of the methods remove the element. They work exactly the same.
If you like our products and services, please help us by posting your review here.

thodoris.bais
Posts: 25
Joined: Sat Jun 03, 2017 4:56 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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()."
If you like our products and services, please help us by posting your review here.

cjgiron
Posts: 14
Joined: Fri Sep 09, 2022 5:03 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
If you like our products and services, please help us by posting your review here.

cjgiron
Posts: 14
Joined: Fri Sep 09, 2022 5:03 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 41 guests