File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Hashtable Problem!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hashtable Problem!!!" Watch "Hashtable Problem!!!" New topic

Hashtable Problem!!!

Arjun Anand

Joined: May 24, 2001
Posts: 25

I'm populating my hashtable with records from a database table where one of the fields is the hashtable key. The problem is, when i print those keys and their values out, they come out in a different order from that of the table. How do i preserve the order of the objects in Hashtable (i.e the objects should be in the same order as that of the table). Or is there any other class which preserves the order as well as maintaining a key-value pair

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Have you considered the TreeMap or TreeSet (java.util)
Ganesh Ram
Ranch Hand

Joined: Jul 20, 2001
Posts: 33
We solved the problem by created a custom class exteding the hashtable class. Here we maintain the order in a vector.
It works great.
Arjun Anand

Joined: May 24, 2001
Posts: 25
Hi Mike,
Thanks for the tip. The TreeMap really works. But again there is a hitch. The TreeMap class takes up too much of memory(it allocates abt 101 hashtable entries) which is just too much for my appl. Is there any way that i can specify the initialy capacity as well as the incremetal capacity.
Originally posted by Mike Curwen:
Have you considered the TreeMap or TreeSet (java.util)

Aleksey Matiychenko
Ranch Hand

Joined: Apr 03, 2001
Posts: 178
I think the constructor should take the capacity number
Try new TreeMap(10)
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Are you sure you're using a TreeMap? A TreeMap does not allocate any extra tree nodes (that would be like allocating extra items on a linked list).
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
Doesn't a TreeMap hold objects in their 'natural order'? This wouldn't necessarily mean they'll be held in the same order as they were on the table, unless they're already held in numeric or alphabetic order there.
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
In jdk1.4 there is a new class LinkedHashMap that preserves the order of the keys in which they were entered.
Arjun Anand

Joined: May 24, 2001
Posts: 25
Could you please explain on what you mean by 'natural order' in TreeMap. Also there is no constructor in TreeMap by which i can specifiy the capacity and/or the load factor. Finally I ended up creating a custom class extending Hashtable with a Vector object in it to store the order in which objects are added to Hashtable.

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Natural order for a class is defined by implementing the Comparable interface. For example, for a String the compareTo() method is defined to reflect alphabetic ordering. It doen't sound like this is what you want though - you want the order in which the elements were added in the first place, which will have nothing to do with the natural order. I don't think TreeMap is any use to you for this.
As David previously noted, there is no use for having any unused nodes on a TreeMap, and therefore no extra capacity or load factor to deal with. A TreeMap is always exactly as big as it needs to be, no bigger.

[This message has been edited by Jim Yingst (edited August 06, 2001).]

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Hashtable Problem!!!
Similar Threads
Sorting Vector of Hashtables by a Hashtable value
Problem Using a Hashtable in a JSP............
HashTable causing Out Of Memory Exception
Sorting and Collection Help
Specify max size of HashTable