Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

List and Set in one  RSS feed

 
Ronwaldo Cruz
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe write a subclass of ArrayList that enforces the uniqueness of elements. There are probably ready-made classes like that out there.
 
Fahd Shariff
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a LinkedHashSet
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!