aspose file tools*
The moose likes Beginning Java and the fly likes List or Set?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "List or Set??" Watch "List or Set??" New topic
Author

List or Set??

Andrew Hartman
Greenhorn

Joined: Mar 18, 2004
Posts: 16
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List or Set??
 
Similar Threads
writing fast, efficient and fine tuned application
Reloading a new version of a class
writing fast, efficient and fine tuned application
Remove duplicates from Collection
How to match a Spring @RequestMapping having a @pathVariable containing “/” ?