This post will discuss how to reverse a string using the Java Collections Framework reverse() method.

The following example demonstrates how to use Collections.reverse() to reverse Java’s string. Following are the complete steps:

  1. Create an empty ArrayList of characters and initialize it with characters of the given string using String.toCharArray().
  2. Reverse the list using java.util.Collections reverse() method.
  3. Finally, convert the ArrayList into String using StringBuilder and return the formed string.

The following program demonstrates it:

Download  Run Code

Output:

The reversed string is !em esreveR

 
The above solution first converts ArrayList into StringBuilder, then StringBuilder into a string. We can directly convert ArrayList into a string by removing square brackets, commas, and single space from it. The following code uses String.replaceAll() to achieve that. But this approach is highly inefficient and should be avoided at all costs.

Download  Run Code

Output:

The reversed string is !em esreveR

That’s all about reversing a String using the reverse() method of Java Collections Framework.