How to sort the values of a HashMap in Java and keep the duplicate entries

This is another way to sort a HashMap. This way is more useful as it sorts the HashMap and keeps the duplicate values as well.

public LinkedHashMap sortHashMapByValuesD(HashMap passedMap) {
    List mapKeys = new ArrayList(passedMap.keySet());
    List mapValues = new ArrayList(passedMap.values());
    Collections.sort(mapValues);
    Collections.sort(mapKeys);
        
    LinkedHashMap sortedMap = 
        new LinkedHashMap();
    
    Iterator valueIt = mapValues.iterator();
    while (valueIt.hasNext()) {
        Object val = valueIt.next();
        Iterator keyIt = mapKeys.iterator();
        
        while (keyIt.hasNext()) {
            Object key = keyIt.next();
            String comp1 = passedMap.get(key).toString();
            String comp2 = val.toString();
            
            if (comp1.equals(comp2)){
                passedMap.remove(key);
                mapKeys.remove(key);
                sortedMap.put((String)key, (Double)val);
                break;
            }

        }

    }
    return sortedMap;
}

fantastic, the best i found

fantastic, the best i found so far

Wow thanks for the great

Wow thanks for the great information. I was searching how to sort Hashmap using Java for quite a long time.I have tried many different ways to do that but your way is definitely the best. Of course the best thing about it is that it keeps the duplicat values. Thanks one more time for this pretty piece of code and I will be waiting for another great ones from you.


Sincerely,

Jeff Markson from java software development

help

hi, may I know how to sort the values by descending order?

How to sort by descending order

bill's picture

In the source code above, after the 5th line...

Collections.sort(mapKeys);
you enter the following simple code:
Collections.reverse(mapValues);

Everything else remains the same. Test it! It should work... report back the result please!

Good solution

Good solution

Gr8

Its a good procedure

Super

Thanks I ve got an issue when create the sorted hashmap but fixed it with the followings sortedMap.put((String)key, val); instead of (double)val thanks

thank you

Thank you this is very informative! It's a good procedure...

its the best way to sort

its the best way to sort HashMap, on internet, coz i had visited minimum 100 sites to find its soln. I found it many where, but its d best.

Perfect

Your code is perfect... I was visited many sites and nothing ... resuming... It's perfect! Thank you very much!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.