wood burning stoves 2.0*
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hashtable Problem" Watch "Hashtable Problem" New topic
Author

Hashtable Problem

Arjun Anand
Greenhorn

Joined: May 24, 2001
Posts: 25
Hi,

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


Arjun
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
Greenhorn

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.
Thanks,
Arjun
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.
http://java.sun.com/j2se/1.4/docs/api/java/util/LinkedHashMap.html
Arjun Anand
Greenhorn

Joined: May 24, 2001
Posts: 25
Paul,
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.

Thanx,
Arjun
Jim Yingst
Wanderer
Sheriff

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

@Ganesh,

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

Joined: Jan 10, 2002
Posts: 61456
    
  67

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.
 
 
subject: Hashtable Problem