Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to sort hash map key and assign values to key

Posted on 2011-09-08
10
Medium Priority
?
471 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
A solution for Fortify Path Manipulation.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:
Suggested Courses

598 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