[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java Hashmap Sorting

Posted on 2007-10-03
8
Medium Priority
?
9,747 Views
Last Modified: 2013-11-24
I have a sorting issue with a Hashmap.  My constraint is that I MUST use the Hashmap and work with existing code.  I do a database query and place the results in a Hashmap.  When I iterate thru the Hashmap, it loses the original alphabetical sorting done by the database.  So, my problem is that I must sort the results coming out of the Hashmap which is then placed into another class.

Here's the code:

Hashmap hm = (database results)
Iterator it =  hm.keySet().iterator();

while (it.hasNext()) {
    String key = (String) it.next();
    STring value = (String) hm.get(key);
                                                                    <<<------------------I must do alphabetical sorting before
                                                                    <<<-----------------placing results (value) into this other class
    some_other_class.addVAlue(new Integer(value), "title");
}
 
There isn't a great number of values (most a couple hundred) so if another loop is okay.
0
Comment
Question by:lcor
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20008247
HashMap has no order. Use LinkedHashMap to preserve order
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20008294
(So if you preserve the order, you won't need to sort it again)
0
 
LVL 13

Accepted Solution

by:
Bart Cremers earned 2000 total points
ID: 20009030
If you need to go with the HashMap for getting the results, wrap them in a TreeMap to get them sorted again:

        TreeMap tm = new TreeMap(getDatabaseResults());
        Iterator it = tm.keySet().iterator();

        while (it.hasNext()) {
            String key = (String) it.next();
            String value = (String) tm.get(key);
            some_other_class.addVAlue(new Integer(value), "title");
        }
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:CEHJ
ID: 20009059
Well of course you don't need to take the performance hit of sorting at all, other than at the db. Just put them in to the LinkedHashMap as they come out of the ResultSet
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20009087
I'd go with CEHJ's solution if you can alter the outcome from the database to a LinkedHashMap. If not you'll need to go with the sorting.
0
 
LVL 92

Expert Comment

by:objects
ID: 20010987
use the follwoiwing:

List list = new ArrayList(hm.values());
Arrays.sort(list);
while (it.hasNext()) {
    String value = (String) it.next();
    some_other_class.addVAlue(new Integer(value), "title");
}
0
 

Author Comment

by:lcor
ID: 20015626
Everyone had great input but I ended up using Bart_Cr's solution in the end.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20016954
What made you want to sort them twice?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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 …
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 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
Course of the Month18 days, 8 hours left to enroll

825 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