Solved

How to sort hash map key and assign values to key

Posted on 2011-09-08
10
406 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
  • 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 250 total points
ID: 36506888
Use TreeMap
0
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 250 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now