wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes List and Set in one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "List and Set in one" Watch "List and Set in one" New topic
Author

List and Set in one

Ronwaldo Cruz
Ranch Hand

Joined: Oct 17, 2006
Posts: 69
Hi,

What do you usually use when you need a collection that is unique (like a Set) but has indexes (like a List)?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
Maybe write a subclass of ArrayList that enforces the uniqueness of elements. There are probably ready-made classes like that out there.


Ping & DNS - my free Android networking tools app
Fahd Shariff
Ranch Hand

Joined: Nov 22, 2002
Posts: 38
Use a LinkedHashSet


Fahd Shariff<br />"Let the code do the talking"
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

Fahd Shariff wrote:Use a LinkedHashSet

While LinkedHashSet will retain the iteration order, it is not indexable. That would have to be added.

I'd still go with Ulf's approach as it's easier to add uniqueness to a List than index-ability to a Set.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Consider Paul's rocket mass heater.
 
subject: List and Set in one
 
Similar Threads
How to sort HashSet containing custom Objects...?
JComboBox: Telling difference if model update or actual select.
Collection Filtering
Error with creation of data source
Intersection of two lists