Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking.I'm 23, born in Romania, student at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff. I know that I look and act differently from most developers, but this is why you will love to work with me! Constantin has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

Sorting a HashMap By Values Without Losing Duplicates

01.05.2012
| 9431 views |
  • submit to reddit

The below method sorts a HashMap by values without removing duplicates. It works for <String,String> but it can be modified accordingly for other types:

private LinkedHashMap sortMapByValuesWithDuplicates(Map 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, (String) val);
                    break;
                }
            }
        }
        return sortedMap;
    }

From http://e-blog-java.blogspot.com/2011/12/sorting-hashmap-by-values-without.html

Published at DZone with permission of its author, Constantin Alin.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Christian Schli... replied on Thu, 2012/01/05 - 4:21am

The Code has several significant flaws. First, it should not modify the passedMap. Second, it has quadratic run time behavior. Third, it compares values by their string representation. Fourth, it doesn't document that it doesn't accept null keys or values. Fifth, it does use raw types. Sixth, it could use an elegant for-loop instead of the clumsy while-construction for brevity.

 To fix the second issue, you should use a Comparator<Map.Entry> for sorting. You could then easily skip your lookup procedure and the string comparison.

Anthony Goubard replied on Thu, 2012/01/05 - 1:53pm

I can see some problems with you solution: you should use interfaces (Map) instead of implementations, a lot of collection manipulation (e.g. 2x sorting), loop inside loop (without using foreach loop), no support for generics.

There are a lot of solutions on stackoverflow from which I still prefer my solution:
http://stackoverflow.com/a/2112659/236152

Anthony Goubard

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.