Skip to content

Commit f7e41c7

Browse files
authored
Merge pull request eugenp#3631 from eugenp/BAEL-1525
refactor streams ex
2 parents 08f08d5 + 7c75954 commit f7e41c7

2 files changed

Lines changed: 27 additions & 24 deletions

File tree

‎core-java/src/main/java/com/baeldung/string/Palindrome.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.baeldung.string;
22

3+
import java.util.stream.IntStream;
4+
35
public class Palindrome {
46

57
public boolean isPalindrome(String text) {
@@ -50,4 +52,11 @@ public boolean isPalindromeRecursive(String text, int forward, int backward) {
5052

5153
return true;
5254
}
55+
56+
public boolean isPalindromeUsingIntStream(String text) {
57+
String temp = text.replaceAll("\\s+", "").toLowerCase();
58+
return IntStream.range(0, temp.length() / 2)
59+
.noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1));
60+
}
61+
5362
}

core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java renamed to core-java/src/test/java/com/baeldung/string/PalindromeTest.java

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.baeldung.string;
22

3-
import java.util.Arrays;
4-
5-
import org.junit.Assert;
3+
import static org.junit.Assert.*;
64
import org.junit.Test;
75

8-
public class WhenCheckingPalindrome {
6+
public class PalindromeTest {
97

108
private String[] words = {
119
"Anna",
@@ -28,50 +26,50 @@ public class WhenCheckingPalindrome {
2826
@Test
2927
public void whenWord_shouldBePalindrome() {
3028
for(String word:words)
31-
Assert.assertTrue(palindrome.isPalindrome(word));
29+
assertTrue(palindrome.isPalindrome(word));
3230
}
3331

3432
@Test
3533
public void whenSentence_shouldBePalindrome() {
3634
for(String sentence:sentences)
37-
Assert.assertTrue(palindrome.isPalindrome(sentence));
35+
assertTrue(palindrome.isPalindrome(sentence));
3836
}
3937

4038
@Test
4139
public void whenReverseWord_shouldBePalindrome() {
4240
for(String word:words)
43-
Assert.assertTrue(palindrome.isPalindromeReverseTheString(word));
41+
assertTrue(palindrome.isPalindromeReverseTheString(word));
4442
}
4543

4644
@Test
4745
public void whenReverseSentence_shouldBePalindrome() {
4846
for(String sentence:sentences)
49-
Assert.assertTrue(palindrome.isPalindromeReverseTheString(sentence));
47+
assertTrue(palindrome.isPalindromeReverseTheString(sentence));
5048
}
5149

5250
@Test
5351
public void whenStringBuilderWord_shouldBePalindrome() {
5452
for(String word:words)
55-
Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(word));
53+
assertTrue(palindrome.isPalindromeUsingStringBuilder(word));
5654
}
5755

5856
@Test
5957
public void whenStringBuilderSentence_shouldBePalindrome() {
6058
for(String sentence:sentences)
61-
Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence));
59+
assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence));
6260
}
6361

6462
@Test
6563
public void whenStringBufferWord_shouldBePalindrome() {
6664
for(String word:words)
67-
Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(word));
65+
assertTrue(palindrome.isPalindromeUsingStringBuffer(word));
6866
}
6967

7068
@Test
7169
public void whenStringBufferSentence_shouldBePalindrome() {
7270
for(String sentence:sentences)
7371

74-
Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence));
72+
assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence));
7573
}
7674

7775
@Test
@@ -80,7 +78,7 @@ public void whenPalindromeRecursive_wordShouldBePalindrome() {
8078
word = word.replaceAll("\\s+", "").toLowerCase();
8179
int backward = word.length()-1;
8280

83-
Assert.assertTrue(palindrome.isPalindromeRecursive(word,0,backward));
81+
assertTrue(palindrome.isPalindromeRecursive(word,0,backward));
8482
}
8583
}
8684

@@ -90,25 +88,21 @@ public void whenPalindromeRecursive_sentenceShouldBePalindrome() {
9088
sentence = sentence.replaceAll("\\s+", "").toLowerCase();
9189
int backward = sentence.length()-1;
9290

93-
Assert.assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward));
91+
assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward));
9492
}
9593
}
9694

9795
@Test
9896
public void whenPalindromeStreams_wordShouldBePalindrome() {
99-
String[] expected = Arrays.stream(words)
100-
.filter(palindrome::isPalindrome)
101-
.toArray(String[]::new);
102-
103-
Assert.assertArrayEquals(expected, words);
97+
for(String word:words) {
98+
assertTrue(palindrome.isPalindromeUsingIntStream(word));
99+
}
104100
}
105101

106102
@Test
107103
public void whenPalindromeStreams_sentenceShouldBePalindrome() {
108-
String[] expected = Arrays.stream(sentences)
109-
.filter(palindrome::isPalindrome)
110-
.toArray(String[]::new);
111-
112-
Assert.assertArrayEquals(expected, sentences);
104+
for(String sentence:sentences) {
105+
assertTrue(palindrome.isPalindromeUsingIntStream(sentence));
106+
}
113107
}
114108
}

0 commit comments

Comments
 (0)