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:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: