File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

List or Set??

Andrew Hartman

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

Joined: Jul 08, 2003
Posts: 24199

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:
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:
subject: List or Set??
It's not a secret anymore!