• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

HashTable data retrieval in reverse order?

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Utilizing a HashTable <Integer, Object> then using Enumeration to retrieve the data to be placed within a Vector.
The data is stored in the required order within the HashTable but retrieval appears to be in reverse order.
Usually with other objects like Arrays and stuff the data in retrieved starting with 0.
Why does HashTable operate in reverse order?
Other than going through some extra steps for reordering what is the method usually used to keep the HashTable order when retrieving?
(I am stuck using the HashTable object.)
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by reverse order? Can you provide us with some example code?
 
Saloon Keeper
Posts: 15730
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hashtable shouldn't have an order. The order in which elements appear depend on the insertion order of the elements, the hash function, the capacity of the table, and maybe some other factors.

It seems you are working with some legacy code. Hashtable, Enumeration and Vector are all obsolete in favor of HashMap, Iterator and List.

If you want to sort the elements in some way, you could do this: Map<Integer, Object> map = new TreeMap<>(table);

You now have a map that contains the same elements as the hash table, sorted by natural order of the keys.
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Hashtable shouldn't have an order. The order in which elements appear depend on the insertion order of the elements, the hash function, the capacity of the table, and maybe some other factors.

It seems you are working with some legacy code. Hashtable, Enumeration and Vector are all obsolete in favor of HashMap, Iterator and List.

If you want to sort the elements in some way, you could do this: Map<Integer, Object> map = new TreeMap<>(table);

You now have a map that contains the same elements as the hash table, sorted by natural order of the keys.



Thanks, that worked great.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic