Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem Using a Hashtable in a JSP............  RSS feed

 
Gabriel Fox
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Pals,
On my JSP i am populating a dropdown(select-option),
as below:

Hashtable hh=new Hashtable();
hh.put(key1,Option1);
hh.put(key2,Option2);
hh.put(key3,Option3);
hh.put(key4,Option4);
where the keys are unique Objects and Options are unique(though optional).
On retrieving the content of this hashtable using:
Enumeration ee=hh.elements();
while( ee.hasMoreElements())
{
ee.nextElement();
//this is sent to output
}

On successfully displaying the content of the Hashtable as a select(drop down), i got the options arranged in an order different from the order they were put in the hashtable using :
hashRef.put(key,Value);
Questions.
1.How does the Hashtable perform ordering of its elements after
putting of some set of key=value pair

2.Does a Hashtable have a set of rules guiding its ordering when
you remove items from say Hashtable-A (storing options in dropdownA)into Hashtable-B(storing options in dropdown-B) on the same JSP page.
Please , i need some explanations or references on these issues.
Cheers.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No hash-based data structure (Hastable, HashMap, HashSet) maintains a specific order. What order there is may change as you add and remove elements. If you want a specific sorting order, use a SortedMap implementation such as TreeMap.
- Peter
 
Gabriel Fox
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter for the nice hint i will get back as soon as i sort
it out.
 
Gabriel Fox
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Peter, i made use of the java.util.TreeMap
which is sorted since it implements the SortedMap.
There were some threading issues which i had to handle because
this implementation is Not thread-safe.
Cheers.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collections.synchronizedSortedMap(SortedMap) is your friend.
- Peter
 
Gabriel Fox
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right Peter.
But, when do we need to worry about multi-threading
issues within a JSP,when a JSP is said to be thread-safe on its
own.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!