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
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: 39549
    
  27
Maybe write a subclass of ArrayList that enforces the uniqueness of elements. There are probably ready-made classes like that out there.


Ping & DNS - updated with new look and Ping home screen widget
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: 60057
    
  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]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List and Set in one
 
Similar Threads
JComboBox: Telling difference if model update or actual select.
Collection Filtering
Intersection of two lists
Error with creation of data source
How to sort HashSet containing custom Objects...?