I have one string say "Aniruddh"
and I want to reverse it using lambdas and streams in Java 8. How can I do it?
We can reverse a string entered by the user using the charAt() method of String class to extract characters from the string and append them in reverse order to reverse the entered string.
String reversed = str. chars() . mapToObj(c -> (char)c) . reduce("", (s,c) -> c+s, (s1,s2) -> s2+s1);
In Java 8, how can we reverse a single string using lambdas and streams? Reversing string in Java can be done in different ways. Even in Java 8 you can use StringBuffer () class and reverse String. However we can also use lamda expressions to do so:- Example below I am taking String array and each String in array is reversed.
Following is another approach to reverse the input String str using Java 8 streams API. String abc = Arrays.asList (str).stream () .map (s -> new StringBuilder (s).reverse ().toString ()) .collect (Collectors.toList ()).get (0);
String class in Java does not have reverse () method, however StringBuilder class has built in reverse () method. 3. StringBuilder class do not have toCharArray () method, while String class does have toCharArray () method. Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Java also has built in reverse () method for the Collections class. Since Collections class reverse () method takes a list object , to reverse the list , we will pass the ArrayList object which is a type of list of characters. 1. We copy String contents to an object of ArrayList.
Given a string like
String str = "Aniruddh";
the canonical solution is
String reversed = new StringBuilder(str).reverse().toString();
If, perhaps for educational purposes, you want to solve this by streaming over the string’s characters, you can do it like
String reversed = str.chars()
.mapToObj(c -> (char)c)
.reduce("", (s,c) -> c+s, (s1,s2) -> s2+s1);
This is not only much more complicated, it also has lots of performance drawbacks.
The following solution eliminates boxing related overhead
String reversed = str.chars()
.collect(StringBuilder::new, (b,c) -> b.insert(0,(char)c), (b1,b2) -> b1.insert(0, b2))
.toString();
but is still less efficient as inserting into the beginning of an array based buffer implies copying all previously collected data.
So the bottom line is, for real applications, stay with the canonical solution shown at the beginning.
Try this for reverse a string using lambda and streams
import java.util.stream.Stream;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
System.out.println(reverse("Anirudh"));;
}
public static String reverse(String string) {
return Stream.of(string)
.map(word->new StringBuilder(word).reverse())
.collect(Collectors.joining(" "));
}
}
If you really want to do it for learning purposes, why not reverse the char array?
public static String reverse(String test) {
return IntStream.range(0, test.length())
.map(i -> test.charAt(test.length() - i - 1))
.collect(StringBuilder::new, (sb, c) -> sb.append((char) c), StringBuilder::append)
.toString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With