• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ordering objects in a hashtable..............  RSS feed

 
Gabriel Fox
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,
I am populating my Hashtable using numeric key from 1 to X, due to an inconsistent sequence in number. I need to display the records in order on screen and this is not possible 'cos the hashtable randomly arranges it's elements. So, is there a way of arranging the objects in a hasttable using it's numeric keys. All ideas are welcomed.
Cheers
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use a Hashtable, use a TreeMap instead; the keys will be stored in numeric order. Note that you don't get amortized linear lookup time, though -- you get ln(N) time.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How sparse is the array? If most of the slots from 1 to X will be filled, then you'd be better off with an ArrayList than any kind of Map.
 
Scott Duncan
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using an arraylist, since it is not a group of key-value pairs, how could you perform a sort without the keys? Based on your question, this doesn't seem to be an option for you. A TreeMap seems to be the best way to go.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key is numeric, so the array index is the key. Again, this is only a good idea if most of the array slots would be filled.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!