how to store and retrieve ordered key value pair objects
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Luca_Paoli
Posted On:   Tuesday, November 19, 2002 01:46 AM

HI, i have a very simple query which retrieves an ORDERED list of items: private static final String sqlRete= "select rete, "+ "des "+ "from table "+ "order by rete"; Then i loop over the connected ResultSet object and store the result in a Properties object as key-value pairs Properties reteProp = new Properties(); ResultSet rs = con.createStatement().executeQuery(sqlRete); while(rs.next()) { reteProp.setProperty(rs.getString("rete"),rs.getString("des"));    More>>

HI, i have a very simple query which retrieves an ORDERED

list of items:


private static final String sqlRete=

"select rete, "+

"des "+

"from table "+

"order by rete";




Then i loop over the connected ResultSet object and

store the result in a Properties object as key-value pairs




Properties reteProp = new Properties();

ResultSet rs = con.createStatement().executeQuery(sqlRete);


while(rs.next())

{

reteProp.setProperty(rs.getString("rete"),rs.getString("des"));

}

Enumeration retePropKeys = reteProp.propertyNames();

String key;

String val;

while(retePropKeys.hasMoreElements())

{

key = (String)retePropKeys.nextElement();

val = reteProp.getProperty(key);

System.out.println("KEY: "+key+" VALUE: "+val);

}




QUESTION: How come when i loop over the Properties object
the items are not in the right order any more?

How can i keep the order and still store the query result
as key-value pairs?


Thanks, clo :))

   <<Less

Re: how to store and retrieve ordered key value pair objects

Posted By:   Lasse_Koskela  
Posted On:   Tuesday, November 19, 2002 03:39 AM

The order being lost is because the structure you're using (Properties) doesn't guarantee maintaining the order...


Basically you need to store key-value pairs in an ordered way. The simplest way would be to do something like this:


// an ordered structure for storing key-value pairs
Collection collection = new ArrayList();
ResultSet rs = ...;
while (rs.next())
{
MyKeyValuePair pair = new MyKeyValuePair(
rs.getString("rete"), rs.getString("des"));
collection.add(pair);
}


Basically, the MyKeyValuePair class stores two strings, key and value. This class could also be replaced by using a two-cell String array with the agreement that the first cell contains the key and the second contains the value.


This approach is good for maintaining the order in the case of the list being accessed in order (i.e. in a loop).


However, if you need to access the values in a random order ("myMap.getValue(someKey)"), you need to implement double-mapping where the key-value pairs are stored both into an ordered structure (Collection, List, etc.) and a map-like structure (Map, Hashtable, Properties, etc.). Then you could access the individual key-value pairs quickly using the map structure, and still obtain an ordered list using the collection/list/whatever structure.

About | Sitemap | Contact