Java 8 – Reverse complete String using Stream and Collectors

In this article, we will learn how to reverse entire/complete String in Java 1.8 version using Stream and Collectors

1. Reverse using Java 8’s Stream & Collectors

  • Initially, we got a String “quick brown fox jumps over lazy dog
  • Map entire String into StringBuilder object and at the same time reversing it using reverse() method of StringBuilder class
  • Finally, collecting reversed String using Stream.collect() method and Collectors.joining() method
  • We will print both original/reversed Strings along with this length

ReverseCompleteStringUsingJava8.java

package in.bench.resources.count.lines.words;

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ReverseCompleteStringUsingJava8 {

	public static void main(String[] args) {

		// sample string
		String str = "quick brown fox jumps over lazy dog";


		// reverse a String using Java 8
		String reverseStr = Stream.of(str)
				.map(string -> new StringBuilder(string).reverse())
				.collect(Collectors.joining());


		// print both strings to console
		System.out.println("Original String = " + str);
		System.out.println("Reversed String = " + reverseStr);


		// print both string length to console
		System.out.println("\n\nOriginal String length = " + str.length());
		System.out.println("Reversed String length = " + reverseStr.length());
	}
}

Output:

Original String = quick brown fox jumps over lazy dog
Reversed String = god yzal revo spmuj xof nworb kciuq


Original String length = 35
Reversed String length = 35

2. Reverse using StringBuilder

  • Initially, we got a String “quick brown fox jumps over lazy dog
  • Create StringBuilder object passing above input string as constructor-argument
  • Then reverse entire/complete String using reverse() method of StringBuilder class
  • Finally, we will print both original/reversed Strings along with this length

ReverseCompleteString.java

package in.bench.resources.count.lines.words;

public class ReverseCompleteString {

	public static void main(String[] args) {

		// sample string
		String inputStr = "quick brown fox jumps over lazy dog";


		// reverse a String
		StringBuilder resultReverse = new StringBuilder(inputStr);
		String reversedStr = resultReverse.reverse().toString().trim();


		// print both strings to console
		System.out.println("Original String = " + inputStr.trim());
		System.out.println("Reversed String = " + reversedStr);


		// print both string length to console
		System.out.println("\n\nOriginal String length = " + inputStr.trim().length());
		System.out.println("Reversed String length = " + reversedStr.length());
	}
}

Output:

Original String = quick brown fox jumps over lazy dog
Reversed String = god yzal revo spmuj xof nworb kciuq


Original String length = 35
Reversed String length = 35

References:

Happy Coding !!
Happy Learning !!

Java - Swapping 2 Strings without Third variable
Java 8 - Reverse each words in a String using Stream and Collectors