googleart
asked on
How to sort hash map key and assign values to key
i have situation where i need to sort hash map values .i am doing it as below
while(resultSet.next()){
String event = resultSet.getString("EVENT TYPE");
int countEvent = resultSet.getInt("COUNTS") ;
map1.put(event,countEvent );
SortedSet<String> sortedset= new TreeSet<String>(map1.keySe t());
System.out.println("sorted set..."+sortedset);
Iterator<String> it = sortedset.iterator();
while (it.hasNext()) {
mapsort =(String)it.next();
num=(map1.get(mapsort));
map4.put(mapsort, num);
// System.out.println ("map sort..."+map4);
}
map3.putAll(map4);
System.out.println("sorted map key..."+mapsort);
System.out.println("sorted map value..."+num);
System.out.println ("map sort..."+map4);
}
Probelm is i am getting sorted set and it's particular value while iterating but when i put that in another hah map sorting order was gone.Can anyone suggest me where i am going wrong?
while(resultSet.next()){
String event = resultSet.getString("EVENT
int countEvent = resultSet.getInt("COUNTS")
map1.put(event,countEvent );
SortedSet<String> sortedset= new TreeSet<String>(map1.keySe
System.out.println("sorted
Iterator<String> it = sortedset.iterator();
while (it.hasNext()) {
mapsort =(String)it.next();
num=(map1.get(mapsort));
map4.put(mapsort, num);
// System.out.println ("map sort..."+map4);
}
map3.putAll(map4);
System.out.println("sorted
System.out.println("sorted
System.out.println ("map sort..."+map4);
}
Probelm is i am getting sorted set and it's particular value while iterating but when i put that in another hah map sorting order was gone.Can anyone suggest me where i am going wrong?
read this - so it will retian the order in which you inserted pairs:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/LinkedHashMap.html
Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(in the first place)
String event = resultSet.getString("EVENTTYPE");
int countEvent = resultSet.getInt("COUNTS");
treeMap.put(event, countEvent);
ASKER
Hi i got it done ,
Chnged hashmap to linked hashmap it worked .thank u
Chnged hashmap to linked hashmap it worked .thank u
>>Chnged hashmap to linked hashmap it worked .thank u
Why would you do that? At the moment, you use a SortedSet to store the key and then
put it into a (now LinkedHash)Map. That's redundant. All you need do is put it straight into a TreeMap
Why would you do that? At the moment, you use a SortedSet to store the key and then
put it into a (now LinkedHash)Map. That's redundant. All you need do is put it straight into a TreeMap
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>LinkedHashMap will keep yoir order
(which is already the natural sort order of the key). Using SortedSet first is redundant
(which is already the natural sort order of the key). Using SortedSet first is redundant
ASKER
Thank you guys for suggestions and i made it working using treemap
:)
I think you can use LinkedHashMap instaed