Given an array of Strings, return the number of sets of anagrams Please review my code:

public class Anagram {

    public static void main(String args[]) {
        String[] arr = { "abc", "cbc", "bcc", "dog", "god", "mary", "army",
                "rty" };
        HashMap&lt;Integer, List&lt;String&gt;&gt; mapList = new HashMap&lt;Integer, List&lt;String&gt;&gt;();

        for (int i = 0; i &lt; arr.length; i++) {
            int hashcode = gethashcode(arr[i]);

            if (mapList != null &amp;&amp; mapList.get(hashcode) == null) {
                List&lt;String&gt; list = new ArrayList&lt;String&gt;();
                mapList.put(hashcode, list);
                list.add(arr[i]);
            } else {
                List&lt;String&gt; list = mapList.get(hashcode);
                list.add(arr[i]);
            }
        }

        printMap(mapList);

        System.out.println("Count=" + mapList.size());
    }

    private static void printMap(HashMap&lt;Integer, List&lt;String&gt;&gt; mapList) {

        if (mapList != null &amp;&amp; mapList.size() &gt; 0) {
            for (Integer key : mapList.keySet()) {
                List&lt;String&gt; list = mapList.get(key);

                System.out.print("[");
                if (list != null &amp;&amp; list.size() &gt; 0) {
                    for (int k = 0; k &lt; list.size(); k++) {
                        System.out.print(list.get(k) + " ");
                    }
                }
                System.out.print("]");
            }
        }

    }

    private static int gethashcode(String str) {
        int hashcode = 0;
        char ch[] = str.toCharArray();

        for (int i = 0; i &lt; ch.length; i++) {
            if (hashcode != 0) {
                hashcode = hashcode + String.valueOf(ch[i]).hashCode();
            } else {
                hashcode = String.valueOf(ch[i]).hashCode();
            }
        }
        return hashcode;
    }
}


Output:


[mary army ][abc ][rty ][cbc bcc ][dog god ]    Count=5
Bug

I fed "aad" and "abc" to your program, and it considered the two strings anagrams of each other.  The problem is that you are trying to use a hashcode to determine whether two strings are anagrams of each other.  However:


Your hashcode is not very good because it simply adds the characters of the string together.  Therefore "aad" and "abc" were considered equivalent.
No matter what, a 32 bit hashcode is going to have some problems because there are more than 2^32 different anagram possibilities if you use long enough strings.  Even a 64 bit hashcode would not be enough.


I suggest that you think about creating a sorted string (i.e. alphabetized) as your hash key instead of a numerical hashcode.

Simplification

This code:


       if (mapList != null &amp;&amp; mapList.get(hashcode) == null) {
            List&lt;String&gt; list = new ArrayList&lt;String&gt;();
            mapList.put(hashcode, list);
            list.add(arr[i]);
        } else {
            List&lt;String&gt; list = mapList.get(hashcode);
            list.add(arr[i]);
        }



could be simplified to:

        List&lt;String&gt; list = mapList.get(hashcode);

        if (list == null) {
            list = new ArrayList&lt;String&gt;();
            mapList.put(hashcode, list);
        }
        list.add(arr[i]);