Reverse a String using Java Collections reverse() method
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:
- Create an empty
ArrayListof characters and initialize it with characters of the given string usingString.toCharArray(). - Reverse the list using
java.util.Collectionsreverse()method. - Finally, convert the
ArrayListinto String usingStringBuilderand return the formed string.
The following program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; class Main { // Method to reverse a string in Java using `Collections.reverse()` public static String reverse(String str) { // base case: if the string is null or empty if (str == null || str.equals("")) { return str; } // create an empty list of characters List<Character> list = new ArrayList<Character>(); // push every character of the given string into it for (char c: str.toCharArray()) { list.add(c); } // reverse list using `java.util.Collections` `reverse()` Collections.reverse(list); // convert `ArrayList` into string and return it return list.stream().map(String::valueOf).collect(Collectors.joining()); } public static void main(String[] args) { String str = "Reverse me!"; // String is immutable str = reverse(str); System.out.println("The reversed string is " + str); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import java.util.List; import java.util.ArrayList; import java.util.Collections; class Main { // Method to reverse a string in Java using `Collections.reverse()` public static String reverse(String str) { // return if the string is null or empty if (str == null || str.equals("")) { return str; } // create an empty list of characters and push every // character of the given string into it List<Character> list = new ArrayList<Character>(); for (char c: str.toCharArray()) { list.add(c); } // reverse list using `java.util.Collections` `reverse()` Collections.reverse(list); // list.toString() is [t, h, g, i, l, e, D, , e, i, h, c, e, T] // convert `list.toString()` to valid string return list.toString() .replaceAll("[,\\[\\]]", "") // t h g i l e D e i h c e T .replaceAll(" ", "@") // t h g i l e D@ e i h c e T .replaceAll(" ", "") // thgileD@eihceT .replaceAll("@", " "); // !em esreveR } public static void main(String[] args) { String str = "Reverse me!"; // String is immutable str = reverse(str); System.out.println("The reversed string is " + str); } } |
Output:
The reversed string is !em esreveR
That’s all about reversing a String using the reverse() method of Java Collections Framework.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)