This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52


Any specific reason for creating custom class and not using LinkedHashMap ?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60794

Considering that the member hasn't posted in over 13 years, I doubt that he's still monitoring this topic.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

Ha ha... Thats a huge time.. I dint notice the date of post.
I agree. Here's the link:
subject: Hashtable Problem
Similar Threads
How to obtain a Map index
How to get values in hashtable
Sorting and Collection Help
Convert set of data into XML document