Solved

How to sort hash map key and assign values to key

Posted on 2011-09-08
10
437 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 250 total points
ID: 36506888
Use TreeMap
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb on wildfly 5 48
jsp error 6 48
DTD and JAVA versions 1 33
Hibernate Inheritance Strategy for Abstract Base class and two concrete classes 3 20
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

730 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