In  this tutorial, we will see “How to Reverse a String using Java 8 Streams?”

How to reverse a string in java 8 streams and collect

Java 8 – How to Reverse a String using Streams? Share on X

/**
 * Using Java 8 Streams, Reverse a String
 * @author Deepak Verma
 *
 */

import java.util.stream.IntStream;

public class Reverse_String_Java8Stream_Example {

	public static void main(String[] args) {

		String str = "Techndeck is an amazing Programming website";
		String reversedStr = reverseTheString(str);

		System.out.println("Original String: " + str);
		System.out.println("Reversed String: " + reversedStr);
	}

	public static String reverseTheString(String input) {
		return IntStream.range(0, input.length())
				        .mapToObj(i -> input.charAt(input.length() - i - 1))
				        .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
				        .toString();
	}
}

 

Output:

Original String: Techndeck is an amazing Programming website
Reversed String: etisbew gnimmargorP gnizama na si kcednhceT

How to reverse a string in java 8 streams and collect

Java 8 – How to Reverse a String using Streams? Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts