?
Solved

How to sort hash map key and assign values to key

Posted on 2011-09-08
10
Medium Priority
?
460 Views
Last Modified: 2012-06-27
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?                  
0
Comment
Question by:googleart
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36506873
HashMap does not support ordere

I think you can use LinkedHashMap instaed
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36506884


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.)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 36506888
Use TreeMap
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 86

Expert Comment

by:CEHJ
ID: 36506896
(in the first place)
String event = resultSet.getString("EVENTTYPE");
                   int countEvent = resultSet.getInt("COUNTS");
                   treeMap.put(event, countEvent);

Open in new window

0
 

Author Comment

by:googleart
ID: 36506924
Hi i got it done ,
Chnged hashmap to linked hashmap it worked .thank u
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36506960
>>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
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1000 total points
ID: 36506979
LinkedHashMap will keep yoir order
TreeMap will impose natural ordering
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36507010
>>LinkedHashMap will keep yoir order

(which is already the natural sort order of the key). Using SortedSet first is redundant
0
 

Author Comment

by:googleart
ID: 36513197
Thank you guys for suggestions and i made it working using treemap
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36513485
:)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question