About Question enthuware.ocpjp.v7.2.1148 :
Moderator: admin
-
- Posts: 12
- Joined: Thu Dec 11, 2014 12:32 am
- Contact:
About Question enthuware.ocpjp.v7.2.1148 :
what is difference between peek(), peekFirst() method of Deque
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1148 :
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()
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.
-
- Posts: 25
- Joined: Sat Jun 03, 2017 4:56 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1148 :
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.
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.
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1148 :
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.
-
- Posts: 25
- Joined: Sat Jun 03, 2017 4:56 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1148 :
Of course:
However, the method (peekFirst()) that is supposed to not remove the head, is actually removing it from the collection.
What am I missing?
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.Retrieves, but does not remove
However, the method (peekFirst()) that is supposed to not remove the head, is actually removing it from the collection.
What am I missing?
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1148 :
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.
-
- Posts: 14
- Joined: Fri Sep 09, 2022 5:03 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1148 :
Hi Admin,
This line in the first option explanation confuses me:
This line in the first option explanation confuses me:
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?push() is a stack method that adds the element to the front.
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1148 :
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
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.
-
- Posts: 14
- Joined: Fri Sep 09, 2022 5:03 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1148 :
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?
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1148 :
Right. See this code:
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.
Code: Select all
Deque<Integer> d = new ArrayDeque<>();
d.add(1);
d.add(2);
//d.push(3);
System.out.println(d.remove());
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.
Who is online
Users browsing this forum: No registered users and 91 guests