This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List or Set??

 
Andrew Hartman
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I will be storing URLs in a collection. But I don't want to have duplicate URLs in my collection. I have read that ArrayLists or LinkedLists can contain duplicate items so i think I should abstain from them.
I think I should use sets, but am sure which one to use. I can't work out which one would be more efficient? I suppose I could also check through my collection using the .contains() method.
Any advice please?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you probably don't need to keep the URLs sorted in some particular order, don't use TreeSet, which adds this ability, but at the cost of some performance. HashSet is usually your best bet.
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with the use of HashSet. This way, you don't have to worry about the overhead of searching the the entire list (worst case), for a URL.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my crib sheet on collections:
http://www.surfscranton.com/architecture/JavaCollections.htm
I know there are other criteria to help choose which one to use. If you have other advice, lemme know and I'll add to this. Frinstance, I should mention that LinkedHashMap retains insertion order.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic