Link to home
Start Free TrialLog in
Avatar of googleart
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("EVENTTYPE");
                   int countEvent = resultSet.getInt("COUNTS");
                        map1.put(event,countEvent );
                  

                  SortedSet<String> sortedset= new TreeSet<String>(map1.keySet());
                  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?                  
Avatar of for_yan
for_yan
Flag of United States of America image

HashMap does not support ordere

I think you can use LinkedHashMap instaed


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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(in the first place)
String event = resultSet.getString("EVENTTYPE");
                   int countEvent = resultSet.getInt("COUNTS");
                   treeMap.put(event, countEvent);

Open in new window

Avatar of googleart
googleart

ASKER

Hi i got it done ,
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>LinkedHashMap will keep yoir order

(which is already the natural sort order of the key). Using SortedSet first is redundant
Thank you guys for suggestions and i made it working using treemap
:)