I know a lot of memory is being used, but how do I make this more efficient in space(and time) .

   public class AnagramsTogether {

    public static String getAnagramsTogether(String sentence){

        if(sentence == null || sentence.trim().equals("")) return null;

        String[] words = sentence.split(" ");
        HashMap<String,List<String>> map =new HashMap<String,List<String>>();

        for(String word:words)
            char[] wordArray = word.toCharArray();

            List<String> tempList = map.get(new String(wordArray));
            if(tempList == null) tempList = new ArrayList<String>(); 
                            map.put(new String(wordArray),tempList);

        StringBuilder result = new StringBuilder();
        for(Map.Entry<String, List<String>> entry : map.entrySet())

            for(String item:entry.getValue()){
                result.append(item+ " ");

        return result.toString();

    public static void main(String[] args) {
         System.out.println(getAnagramsTogether("cat dog act tac god"));


3 Answers 3


Whitespace discipline

While your indentation is good, the placement of whitespace isn't very good yet. In Java, we tend to put whitespace after keywords and before braces. A beautifier with standard settings would make your code more readable already.

Use the most common type that still does the job.

For example, your variable map should be declared as Map, not HashMap.

Smarter Use of Map API

Instead of getting the list, checking whether it's null and if it isn't null, creating it, and then always putting it, you could use Map.computeIfAbsent(), like this:

    .computeIfAbsent(normalizedAnagram, s -> ArrayList::new)

Split your logic

Your function getAnagramsTogether is doing many things. You could split it into multiple methods. For example, you could have a separate method normalizeAnagram() which takes a String and returns a new String with the characters sorted.

Bug in your call to Arrays.sort(char[] wordArray)

It should be noted that char is NOT a character. It is a UTF-16 fragment. For the first Unicode plane, i.e. code points with values that can be represented by 16 bits, char is a character. But for code points with higher values, a char contains only part of the character, and the way how you construct the normalized anagram is tearing characters apart.

It may not matter to you, as probably all relevant words in your use cases would have code points in the 16 bit plane.

Don't use + in StringBuilder.append()

You should be consistent, either use + or append(). But using + inside StringBuilder.append() doesn't make sense. We use StringBuilder in order to have String-concatenation which is faster and uses less memory than using +. Using + in an argument to StringBuilder.append() is completely contrary to the intention of using StringBuilder in the first place.

Don't construct the same object twice.

You're calling new String(wordArray) twice for no good reason. You could just create a single String normalizedAnagram and use that.

Consider a smarter way of String concatenation.

The following code concatenates your final Map into a String in a smarter way:

    .flatMap(entry -> entry.getValue().stream())
    .collect(Collectors.joining(" "))

Use String.isEmpty instead of .equals("")

if(sentence == null || sentence.trim().equals("")) return null;

Comparing a string with a empty string is slower than using isEmpty() because the first step in comparing a string getting the size of BOTH strings.

if(sentence == null || sentence.trim().isEmpty()) return null;

Returning null

You should throw an exception instead of returning null. Returning null is almost never a good idea.

Using java 8:

If you have java 8 available you can use streams:

   public static String getAnagramsTogether2(String sentence)
       if(sentence == null ||  sentence.trim().isEmpty()) 
           throw new IllegalArgumentException();

       Map<String, List<String>> map =Arrays.stream(sentence.split(" "))

       return map.values()
                 .collect(Collectors.joining(" "));
   public static String sortString(String string) {

       return  string.chars()
  • \$\begingroup\$ i'm kinda new to java 8 but this reduces the size of code like crazy !:) \$\endgroup\$ Feb 14, 2016 at 4:39
  • 1
    \$\begingroup\$ @MAG Do you mind explaining the "magic" of sortString? \$\endgroup\$
    – AlexR
    Feb 14, 2016 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.