Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Difference between remove and pop  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm studying for the OCP. From what I understand, a Deque has three methods that can remove an element from the left of a deque:
1. poll() - returns null for an empty list
2. remove() - throws an exception for an empty list
3. pop() - throws an exception for an empty list

Is there a difference between remove() and pop() when dealing with a Deque?

Thanks
 
author & internet detective
Posts: 39156
724
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
Welcome to CodeRanch!

There's also removeFirst().

The two methods you mention do the same thing. There are two because they match the way people talked about single and double queues.
 
Marshal
Posts: 63435
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

There is also a pop() method because the functionality of a Stack has been mixed in with a Deque, so the push(), pop(), and peek() methods, which are not usually Queue/Deque methods, are added here.
 
Sheriff
Posts: 21642
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there is a difference. I've checked the four known Deque implementations (ArrayDeque, ConcurrentLinkedDeque, LinkedBlockingDeque, LinkedList), and all delegate to removeFirst from both the remove and pop methods.
 
Campbell Ritchie
Marshal
Posts: 63435
205
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only difference is their source, because Sun managed to conflate stacks and queues when they designed ArrayDeque in Java6. Those of us who have done stack programming would have this sort of inheritance hierarchyCounting errors in that diagram is left as an exercise to the reader

The Queue/Deque interfaces would have offer() poll() peek() and similar methods; the Stack interface would have push() pop() peek() methods and wouldn't extend Collection. Unfortunately they shot themselves in the foot with public class Stack extends Vector and they have never recovered from that error. The pop() method comes from the usual concepts of a stack, and that is the one difference.

I notice that peek() seems to appear in both queues and stacks, but there is a difference between the two versions of peek(), too. that in a Queue seems to return null from an empty Queue and that in the Stack class throws an exception.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!