Detecting if two strings are anagrams using TreeMap
Replace the TreeMap with a HashMap since a TreeMap offers \$O(log(n))\$ lookup (containsKey and get) and insertion (put) time whereas a HashMap offers amortized constant or \$O(1)\$ time for those same methods.

The time complexity of this algorithm (with a HashMap) is in fact O(length(s1)) or rather \$O(n)\$ because the contains, add and remove methods of a HashMap work in amortized constant or \$O(1)\$ time (it is \$O(n)\$ when the internal arrays have to be expanded).

You can make the code cleaner by getting rid of all those ifs in the loop. The get method returns null if the key doesn't exist, which helps us remove that containsKey call.

    for ( int i=0 ; i&lt;s1.length() ; i++ ) {
        Integer freq = charFrequencyS1.get(s1.charAt(i));
        charFrequencyS1.put(s1.charAt(i), freq == null ? 1 : freq.intValue() + 1);

        Integer freq = charFrequencyS2.get(s2.charAt(i));
        charFrequencyS2.put(s2.charAt(i), freq == null ? 1 : freq.intValue() + 1);
    }


You could even go a step further and extract those two lines into a method, like

void addCharToMap(Map&lt;Character, Integer&gt; charMap, Character char) {
    Integer freq = charMap.get(char);
    charMap.put(char, freq == null ? 1 : freq.intValue() + 1);
}


And use the above method twice.

One thing to note is that if you are sure that your input is going to be numeric or alphanumeric, you can set the initialCapacity of the HashMap to be 26 or 36 respectively, because that amount is the maximum number of possible unique characters in the strings.