• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Ordering objects in a hashtable..............

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic