I am using JSP, Servlets. After quering in servlets, putting data in HashTable then set attribute in servlets, get attribute in JSP & retrieving data from HashTable in JSP & putting in select html element, data is not coming in ascending order in select drop down box. I need A,B,C,D but it is coming b,d,c,a. When I run query in DB, it shows in sequence but after putting in HashTable, it is not coming in sequence.
How to make it ascending?
Looking forward for an early reply.
This topic has been locked.
7 replies Latest Post - 2012-08-15T17:43:01Z by gr8yash
Pinned topic Doesn't Sort data in Ascending order in HashTable?
Answered question This question has been answered.
Unanswered question This question has not been answered yet.
Updated on 2012-08-15T17:43:01Z at 2012-08-15T17:43:01Z by gr8yash
Re: Doesn't Sort data in Ascending order in HashTable?2006-03-08T20:53:09Z in response to SystemAdminHashtable doesn't make any guarantees about order. Generally, when you need a dynamically key-sorted order, you use a TreeMap. In your case, since you are getting the data in sequence ( and, I assume, not adding specific elements later that need to be ordered, ) you can use a LinkedHashMap which guarantees insertion order and is faster than a TreeMap. See Map Implementations for more information.
Another possible alternative is correlated arrays or ArrayLists.
Re: Doesn't Sort data in Ascending order in HashTable?2006-03-09T08:04:00Z in response to SystemAdminHi
I tried with TreeSet but it didn't work. Here is the code :
In servlet :
Connection lConnection = getConnection(pRequest);
String lSQL = "";
Hashtable lLtypeHashtable = new Hashtable();
lStatement = lConnection.createStatement();
lSQL = "SELECT RCID,RCMEANING FROM REFERENCECODES WHERE RCDOMAIN = 'LOCATIONTYPE' AND RCROWSTATE > 0 order by RCMEANING";
lResultSet = lStatement.executeQuery(lSQL);
String lRcid = lResultSet.getString(1);
String lRcmeaning = lResultSet.getString(2);
if(lResultSet != null) lResultSet.close();
if(lStatement != null) lStatement.close();
//Below Query is executed when one data from select element is selected
String lLType = DisplayUtilities.getString(pRequest.getParameter("LType"),true);
//LType is name of select element in JSP.
if (lLType != null)
lSQL = lSQL + " AND " + lUpperCaseFunction + "(LOCATIONTYPE)" +
" = " + DBUtilities.formatString(lLType.toUpperCase());
In JSp :
Hashtable lLtypeHashtable = (Hashtable)request.getAttribute("LtypeHashtable");
<select name="LType" size="1" >
<option Value="">< Select ></option>
if(lLtypeHashtable != null)
Enumeration enum = lLtypeHashtable.keys();
String key = (String)enum.nextElement();
String value = (String)lLtypeHashtable.get(key);
String flagBack = "";
flagBack = "selected";
<option Value="<%=key%>" <%=flagBack%>><%=value%></option>
How should I implement TreeSet?
Looking forward for an early reply.
Re: Doesn't Sort data in Ascending order in HashTable?2006-03-09T13:32:10Z in response to SystemAdminI tried thru TreeMap but its sorting thru key(ID) & not values. I need alphabetically sorting of values(data) in select drop down box such that in future also some new topics are added to select, it should show alphabetically in select box.
Re: Doesn't Sort data in Ascending order in HashTable?2006-03-10T12:31:47Z in response to SystemAdminAs I indicated in my initial response, LinkedHashMap "guarantees insertion order and is faster than a TreeMap." That should work for you. The values are pulled from the database in the order you want them, so LinkedHashMap's insertion order here is also value-alphabetical. The code example you sent still uses HashTable.
Re: Doesn't Sort data in Ascending order in HashTable?2006-03-11T10:51:32Z in response to SystemAdminI can't use LinkedHashMap because data is not in order in DB & in future more data can be added in select box.
Anyway, Thanks, problem got solved. It happened by implementing Comparator interface, comparing values, LinkedList, ListIterator.