In this post, we will see “How to reverse characters of a word and also how to reverse words in a String in Java 8?”. To achieve that, we are going to use Stream API introduced in Java 8 and StringBuilder to reverse the string.
See what we are looking to achieve here:
1 2 3 4 5 6 7 8 9 10 | Reversing Character of a Word: Original word: Welcome Reversed word: emocleW Reversing Word in a String: Original String: Welcome to Techndeck.com Reversed String: emocleW ot moc.kcednhceT |
Reverse Character & Word in a String in Java 8 with example...!!! Share on X
In this tutorial, we are going to cover below topics:
- How to reverse Character of a Word?
- How to reverse Word in a String?
Example 1. Java code to reverse character in a word
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import java.util.Arrays; import java.util.function.Function; import java.util.stream.Stream; /** * To reverse word characters using Java 8 * * @author Deepak Verma * */ public class Reverse_Character_Word_String_Java8 { public static void main(String[] args) { Reverse_Character_Word_String_Java8 obj = new Reverse_Character_Word_String_Java8(); obj.reverse_character_in_words("Welcome to Techndeck.com"); } /** * To reverse character of a word using Java 8 * * @param str */ public void reverse_character_in_words(String str) { String words[] = str.split(" "); Stream <String> streamOfWords = Arrays.stream(words); streamOfWords.forEach(w -> { System.out.println("Original word: " + w); Function <String,String> reverse = s -> new StringBuilder(s).reverse().toString(); String reversedWord = reverse.apply(w); System.out.println("Reversed word: " + reversedWord + "\n"); }); } } |
Output:
1 2 3 4 5 6 7 8 | Original word: Welcome Reversed word: emocleW Original word: to Reversed word: ot Original word: Techndeck.com Reversed word: moc.kcednhceT |
Example 2. Java code to reverse word in a string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import java.util.Arrays; import java.util.function.Function; import java.util.stream.Stream; /** * To reverse words in a string using Java 8 * * @author Deepak Verma * */ public class Reverse_Word_In_String_Java8 { public static void main(String[] args) { Reverse_Word_In_String_Java8 obj = new Reverse_Word_In_String_Java8(); obj.reverse_words_in_String("Welcome to Techndeck.com"); } /** * To reverse words in a String using Java 8 * * @param str */ public void reverse_words_in_String(String str) { String words[] = str.split(" "); StringBuilder sb = new StringBuilder(); Stream <String> stream = Arrays.stream(words); stream.forEach(w - > { Function <String,String> reverse = s -> new StringBuilder(s).reverse().toString(); String reversedWord = reverse.apply(w); sb.append(reversedWord + " "); }); System.out.println("Original String: " + str); System.out.println("Reversed String: " + sb); } } |
Output:
1 2 | Original String: Welcome to Techndeck.com Reversed String: emocleW ot moc.kcednhceT |
Reverse Character & Word in a String in Java 8 with example...!!! Share on X
Do you like this Post? – then check my other helpful posts:
- How to pass function as a parameter in Java 8
- How to sort a stream of numbers/strings in a list in Java 8