• 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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: 39076
714
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: 62231
193
  • 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: 21556
100
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: 62231
193
  • 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.
 
We're all out of roofs. But we still have tiny ads:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!