Skip to content

Commit 50deec5

Browse files
committed
refactor streams ex
1 parent 87a3677 commit 50deec5

2 files changed

Lines changed: 26 additions & 22 deletions

File tree

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

Lines changed: 8 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,10 @@ 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+
}
5361
}

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 & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import java.util.Arrays;
44

5-
import org.junit.Assert;
5+
import static org.junit.Assert.*;
66
import org.junit.Test;
77

8-
public class WhenCheckingPalindrome {
8+
public class PalindromeTest {
99

1010
private String[] words = {
1111
"Anna",
@@ -28,50 +28,50 @@ public class WhenCheckingPalindrome {
2828
@Test
2929
public void whenWord_shouldBePalindrome() {
3030
for(String word:words)
31-
Assert.assertTrue(palindrome.isPalindrome(word));
31+
assertTrue(palindrome.isPalindrome(word));
3232
}
3333

3434
@Test
3535
public void whenSentence_shouldBePalindrome() {
3636
for(String sentence:sentences)
37-
Assert.assertTrue(palindrome.isPalindrome(sentence));
37+
assertTrue(palindrome.isPalindrome(sentence));
3838
}
3939

4040
@Test
4141
public void whenReverseWord_shouldBePalindrome() {
4242
for(String word:words)
43-
Assert.assertTrue(palindrome.isPalindromeReverseTheString(word));
43+
assertTrue(palindrome.isPalindromeReverseTheString(word));
4444
}
4545

4646
@Test
4747
public void whenReverseSentence_shouldBePalindrome() {
4848
for(String sentence:sentences)
49-
Assert.assertTrue(palindrome.isPalindromeReverseTheString(sentence));
49+
assertTrue(palindrome.isPalindromeReverseTheString(sentence));
5050
}
5151

5252
@Test
5353
public void whenStringBuilderWord_shouldBePalindrome() {
5454
for(String word:words)
55-
Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(word));
55+
assertTrue(palindrome.isPalindromeUsingStringBuilder(word));
5656
}
5757

5858
@Test
5959
public void whenStringBuilderSentence_shouldBePalindrome() {
6060
for(String sentence:sentences)
61-
Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence));
61+
assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence));
6262
}
6363

6464
@Test
6565
public void whenStringBufferWord_shouldBePalindrome() {
6666
for(String word:words)
67-
Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(word));
67+
assertTrue(palindrome.isPalindromeUsingStringBuffer(word));
6868
}
6969

7070
@Test
7171
public void whenStringBufferSentence_shouldBePalindrome() {
7272
for(String sentence:sentences)
7373

74-
Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence));
74+
assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence));
7575
}
7676

7777
@Test
@@ -80,7 +80,7 @@ public void whenPalindromeRecursive_wordShouldBePalindrome() {
8080
word = word.replaceAll("\\s+", "").toLowerCase();
8181
int backward = word.length()-1;
8282

83-
Assert.assertTrue(palindrome.isPalindromeRecursive(word,0,backward));
83+
assertTrue(palindrome.isPalindromeRecursive(word,0,backward));
8484
}
8585
}
8686

@@ -90,25 +90,21 @@ public void whenPalindromeRecursive_sentenceShouldBePalindrome() {
9090
sentence = sentence.replaceAll("\\s+", "").toLowerCase();
9191
int backward = sentence.length()-1;
9292

93-
Assert.assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward));
93+
assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward));
9494
}
9595
}
9696

9797
@Test
9898
public void whenPalindromeStreams_wordShouldBePalindrome() {
99-
String[] expected = Arrays.stream(words)
100-
.filter(palindrome::isPalindrome)
101-
.toArray(String[]::new);
102-
103-
Assert.assertArrayEquals(expected, words);
99+
for(String word:words) {
100+
assertTrue(palindrome.isPalindromeUsingIntStream(word));
101+
}
104102
}
105103

106104
@Test
107105
public void whenPalindromeStreams_sentenceShouldBePalindrome() {
108-
String[] expected = Arrays.stream(sentences)
109-
.filter(palindrome::isPalindrome)
110-
.toArray(String[]::new);
111-
112-
Assert.assertArrayEquals(expected, sentences);
106+
for(String sentence:sentences) {
107+
assertTrue(palindrome.isPalindromeUsingIntStream(sentence));
108+
}
113109
}
114110
}

0 commit comments

Comments
 (0)