Introduction

Reversing the order of words in a sentence (while keeping each word intact) is a common string manipulation task.


Method 1: Using split() and Collections.reverse()

 
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ReverseWordOrder {
    
    public static String reverseWordOrder(String sentence) {
        if (sentence == null || sentence.isEmpty()) {
            return sentence;
        }
        
        String[] words = sentence.split(" ");
        List<String> wordList = Arrays.asList(words);
        Collections.reverse(wordList);
        
        return String.join(" ", wordList);
    }
    
    public static void main(String[] args) {
        System.out.println(reverseWordOrder("Hello World Java"));     // Java World Hello
        System.out.println(reverseWordOrder("The Quick Brown Fox"));  // Fox Brown Quick The
    }
}
 

Output:

 
Java World Hello
Fox Brown Quick The
 

Method 2: Using Streams

 
import java.util.Arrays;

public class ReverseWordOrderStream {
    
    public static String reverseWordOrder(String sentence) {
        return Arrays.stream(sentence.split(" "))
                    .reduce((s1, s2) -> s2 + " " + s1)
                    .orElse("");
    }
    
    public static void main(String[] args) {
        System.out.println(reverseWordOrder("Java Programming"));
    }
}
 

Method 3: Using Stack

 
import java.util.Stack;

public class ReverseWordOrderStack {
    
    public static String reverseWordOrder(String sentence) {
        String[] words = sentence.split(" ");
        Stack<String> stack = new Stack<>();
        
        for (String word : words) {
            stack.push(word);
        }
        
        StringBuilder result = new StringBuilder();
        while (!stack.isEmpty()) {
            result.append(stack.pop());
            if (!stack.isEmpty()) {
                result.append(" ");
            }
        }
        
        return result.toString();
    }
    
    public static void main(String[] args) {
        System.out.println(reverseWordOrder("Test String Example"));
    }
}
 

Frequently Asked Questions

Q1. How do I preserve multiple spaces?

Answer: Use regex:

Advertisement
 
split("(?<=\\s)(?=\\S)|(?<=\\S)(?=\\s)")
 

Q2. Which method is fastest?

Answer: Collections.reverse() is most efficient.

Q3. Can I modify a sentence in place?

Answer: No, must create new string.

Q4. How do I handle punctuation?

Answer: Keep it with words or handle separately.

Q5. Performance comparison?

Answer: All O(n), Collections.reverse() fastest.

Q6. What about empty words?

Answer: Filter with:

 
!word.isEmpty()
 

Q7. Unicode support?

Answer: Yes, all methods support Unicode.

Q8. How to reverse only part of sentence?

Answer: Extract substring, reverse, reinsert.

Q9. Memory usage?

Answer: Stack method uses more memory.

Q10. Large sentence performance?

Answer: Collections.reverse() scales best.


Conclusion

For reversing word order:

  • Best: Collections.reverse()
  • Simple: Split and join
  • Alternative: Stack-based approach

Master this for text processing tasks.