Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between add() and addList() methods?

 
kenny gill
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have no idea where to find the information for this question and could use some advice:

The API says that add(E e) and addLast(E e) are equivalent, but there is a difference. What is it?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing you're talking about the java.util.Deque interface. That would have been useful to mention, I had to hunt around the javadocs a bit to find which collection class/interface it might be :-)

If you have an object implementing Deque, then both methods will do the same. The difference is that using add will allow you to use an object that implements Queue instead, since add is part of both interfaces, whereas addLast is only part of Dequeue.

Why would you want to do this? Well, there may come a day when you decide that you didn't need to use a Deque after all, and would like to switch to Queue instead (maybe in order to use other collection classes that implement Queue, but not Deque). Had you used addLast, then you would need to refactor your code to use add instead; if you had used add from the beginning, then there is no need to change anything.
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote: . . . .

I have updated the subject for you -- please use it as a guide for future topics. Thanks.
Thank you, Bear. But I think you introduced a spectacular spelling error
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BEWARE ALL!! Ma main man Ritchie is the mood for correcting everyone's grammar and composition today .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic