StringBuffer example. Where you have to append a lot of times.
Code
/**
* Store each work in map then reverse the whole map
*/
public String reverseWords(String s) {
String[] words = s.trim().split("[ ]+");
StringBuilder result = new StringBuilder();
if (words.length > 0) {
for (int i = words.length - 1; i > 0; i--) {
result.append(words[i]);
result.append(" ");
}
result.append(words[0]);
}
return result.toString();
}