BAEL-6435: Reverse a String in Java 8 using Lambda and Streams (#13910)
This commit is contained in:
parent
18834b349e
commit
7d86466fb7
@ -1,5 +1,9 @@
|
||||
package com.baeldung.reverse;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class ReverseStringExamples {
|
||||
@ -46,11 +50,43 @@ public class ReverseStringExamples {
|
||||
}
|
||||
|
||||
return output.toString()
|
||||
.trim();
|
||||
.trim();
|
||||
}
|
||||
|
||||
public static String reverseTheOrderOfWordsUsingApacheCommons(String sentence) {
|
||||
return StringUtils.reverseDelimited(sentence, ' ');
|
||||
}
|
||||
|
||||
public static String reverseUsingIntStreamRangeMethod(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
char[] charArray = str.toCharArray();
|
||||
return IntStream.range(0, str.length())
|
||||
.mapToObj(i -> charArray[str.length() - i - 1])
|
||||
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
|
||||
.toString();
|
||||
}
|
||||
|
||||
public static String reverseUsingStreamOfMethod(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Stream.of(str)
|
||||
.map(string -> new StringBuilder(string).reverse())
|
||||
.collect(Collectors.joining());
|
||||
}
|
||||
|
||||
public static String reverseUsingCharsMethod(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return str.chars()
|
||||
.mapToObj(c -> (char) c)
|
||||
.reduce("", (a, b) -> b + a, (a2, b2) -> b2 + a2);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,11 @@
|
||||
package com.baeldung.reverse;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ReverseStringExamplesUnitTest {
|
||||
|
||||
private static final String STRING_INPUT = "cat";
|
||||
@ -19,7 +20,7 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverse(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@ -30,7 +31,7 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingStringBuilder(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@ -41,7 +42,7 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingApacheCommons(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@ -52,7 +53,7 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverseTheOrderOfWords(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(REVERSED_WORDS_SENTENCE, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@ -63,7 +64,40 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverseTheOrderOfWordsUsingApacheCommons(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(REVERSED_WORDS_SENTENCE, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseStringUsingIntStreamRangeMethod_ThenCorrectStringIsReturned() {
|
||||
String reversed = ReverseStringExamples.reverseUsingIntStreamRangeMethod(STRING_INPUT);
|
||||
String reversedNull = ReverseStringExamples.reverseUsingIntStreamRangeMethod(null);
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingIntStreamRangeMethod(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseStringUsingCharsMethod_ThenCorrectStringIsReturned() {
|
||||
String reversed = ReverseStringExamples.reverseUsingCharsMethod(STRING_INPUT);
|
||||
String reversedNull = ReverseStringExamples.reverseUsingCharsMethod(null);
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingCharsMethod(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseStringUsingStreamOfMethod_ThenCorrectStringIsReturned() {
|
||||
String reversed = ReverseStringExamples.reverseUsingStreamOfMethod(STRING_INPUT);
|
||||
String reversedNull = ReverseStringExamples.reverseUsingStreamOfMethod(null);
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingStreamOfMethod(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user