String Reverse Example In Java

Table of Contents

Introduction

This tutorial will show you string reverse example in Java. You can use various ways to reverse a string in Java and I have presented here five ways to reverse a string. String class in Java is an immutable class and that’s why it does not have reverse() method to reverse the given string.

I will use here StringBuilder and StringBuffer‘s reverse() method, I will build my own custom method, recursion with substring() method and Java 8’s Lambda and Stream API to reverse the given string.

Prerequisites

Java 1.8

Reverse A String

I will show here different ways to reverse a given string in Java programming language.

StringBuffer’s reverse() Method

I will use StringBuffer‘s reverse() method to reverse the given string. Use StringBuffer class when your object is shared among multiple threads otherwise use StringBuilder for better performance.

public String usingStringBufferReverse(final String string) {
	if (string == null || string.isEmpty()) {
		return string;
	}
	String reverseString = new StringBuffer(string).reverse().toString();
	return reverseString;
}

StringBuilder’s reverse() Method

I will use StringBuilder‘s reverse() method to reverse the given string. Use StringBuilder when an object is not shared among multiple threads.

public String usingStringBuilderReverse(final String string) {
	if (string == null || string.isEmpty()) {
		return string;
	}
	String reverseString = new StringBuffer(string).reverse().toString();
	return reverseString;
}

Writing Custom Method

I will write my own method in Java to reverse a given string. I will use charAt() method from String class.

public String usingCustomReverse(final String string) {
	if (string == null || string.isEmpty()) {
		return string;
	}
	String reverse = "";
	for (int i = string.length() - 1; i >= 0; i--) {
		reverse = reverse + string.charAt(i);
	}
	return reverse;
}

Using Recursion

I will use recursion using substring() method to reverse a given string.

public String usingRecursion(final String string) {
	if (string == null || string.isEmpty() || string.length() < 2) {
		return string;
	}
	return usingRecursion(string.substring(1)) + string.charAt(0);
}

Using Java 8’s Lambda & Stream API

We will use Java 8’s Lambda and Stream API to reverse a given string.

public String usingJava8LambdaStream(final String string) {
	return IntStream.range(0, string.length()).map(i -> string.charAt(string.length() - i - 1))
			.collect(StringBuilder::new, (s, c) -> s.append((char) c), StringBuilder::append).toString();
}

Another way of reversing string using Java’s Lambda and Stream API is given below:

public String usingAnotherJava8LambdaStream(final String string) {
	return Stream.of(string).map(word -> new StringBuilder(word).reverse()).collect(Collectors.joining(" "));
}

Java Main Class

Create a Java main class to test above methods to reverse a string.

package com.roytuts.stringreverse;

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

public class StringReverseExample {

	public static void main(String[] args) {
		StringReverseExample sr = new StringReverseExample();

		String word = "HelloWorld";

		// quick wasy to reverse String in Java - Use StringBuffer
		String reverse = sr.usingStringBufferReverse(word);
		System.out.printf(" original String : %s , reversed String : %s  %n", word, reverse);

		// another quick to reverse String in Java - use StringBuilder
		reverse = sr.usingStringBuilderReverse(word);
		System.out.printf(" original String : %s , reversed String : %s %n", word, reverse);

		// one way to reverse String using custom method
		reverse = sr.usingCustomReverse(word);
		System.out.printf(" original String : %s , reversed String : %s %n", word, reverse);

		// reverse String using recursion
		reverse = sr.usingRecursion(word);
		System.out.printf(" original String : %s , reversed String : %s %n", word, reverse);

		// reverse String using Java 8 IntStream
		reverse = sr.usingJava8LambdaStream(word);
		System.out.printf(" original String : %s , reversed String : %s %n", word, reverse);

		// reverse String using Java 8 Stream
		reverse = sr.usingAnotherJava8LambdaStream(word);
		System.out.printf(" original String : %s , reversed String : %s %n", word, reverse);
	}

	public String usingStringBufferReverse(final String string) {
		if (string == null || string.isEmpty()) {
			return string;
		}
		String reverseString = new StringBuffer(string).reverse().toString();
		return reverseString;
	}

	public String usingStringBuilderReverse(final String string) {
		if (string == null || string.isEmpty()) {
			return string;
		}
		String reverseString = new StringBuffer(string).reverse().toString();
		return reverseString;
	}

	public String usingCustomReverse(final String string) {
		if (string == null || string.isEmpty()) {
			return string;
		}
		String reverse = "";
		for (int i = string.length() - 1; i >= 0; i--) {
			reverse = reverse + string.charAt(i);
		}
		return reverse;
	}

	public String usingRecursion(final String string) {
		if (string == null || string.isEmpty() || string.length() < 2) {
			return string;
		}
		return usingRecursion(string.substring(1)) + string.charAt(0);
	}

	public String usingJava8LambdaStream(final String string) {
		return IntStream.range(0, string.length()).map(i -> string.charAt(string.length() - i - 1))
				.collect(StringBuilder::new, (s, c) -> s.append((char) c), StringBuilder::append).toString();
	}

	public String usingAnotherJava8LambdaStream(final String string) {
		return Stream.of(string).map(word -> new StringBuilder(word).reverse()).collect(Collectors.joining(" "));
	}

}

Testing String Reverse

Run the above main class, you will see below output into the console:

 original String : HelloWorld , reversed String : dlroWolleH  
 original String : HelloWorld , reversed String : dlroWolleH 
 original String : HelloWorld , reversed String : dlroWolleH 
 original String : HelloWorld , reversed String : dlroWolleH 
 original String : HelloWorld , reversed String : dlroWolleH 
 original String : HelloWorld , reversed String : dlroWolleH

Source Code

Download

Leave a Reply

Your email address will not be published.