From 50deec5b5cb8c1ae7d732ffd5726933c1e98f956 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 10 Feb 2018 17:42:52 +0200 Subject: [PATCH 1/3] refactor streams ex --- .../java/com/baeldung/string/Palindrome.java | 8 ++++ ...ingPalindrome.java => PalindromeTest.java} | 40 +++++++++---------- 2 files changed, 26 insertions(+), 22 deletions(-) rename core-java/src/test/java/com/baeldung/string/{WhenCheckingPalindrome.java => PalindromeTest.java} (63%) diff --git a/core-java/src/main/java/com/baeldung/string/Palindrome.java b/core-java/src/main/java/com/baeldung/string/Palindrome.java index 03b65d1a84..d70a89525a 100644 --- a/core-java/src/main/java/com/baeldung/string/Palindrome.java +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -1,5 +1,7 @@ package com.baeldung.string; +import java.util.stream.IntStream; + public class Palindrome { public boolean isPalindrome(String text) { @@ -50,4 +52,10 @@ public class Palindrome { return true; } + + public boolean isPalindromeUsingIntStream(String text) { + String temp = text.replaceAll("\\s+", "").toLowerCase(); + return IntStream.range(0, temp.length() / 2) + .noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1)); + } } diff --git a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java similarity index 63% rename from core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java rename to core-java/src/test/java/com/baeldung/string/PalindromeTest.java index e3655a3140..ef78a2d840 100644 --- a/core-java/src/test/java/com/baeldung/string/WhenCheckingPalindrome.java +++ b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java @@ -2,10 +2,10 @@ package com.baeldung.string; import java.util.Arrays; -import org.junit.Assert; +import static org.junit.Assert.*; import org.junit.Test; -public class WhenCheckingPalindrome { +public class PalindromeTest { private String[] words = { "Anna", @@ -28,50 +28,50 @@ public class WhenCheckingPalindrome { @Test public void whenWord_shouldBePalindrome() { for(String word:words) - Assert.assertTrue(palindrome.isPalindrome(word)); + assertTrue(palindrome.isPalindrome(word)); } @Test public void whenSentence_shouldBePalindrome() { for(String sentence:sentences) - Assert.assertTrue(palindrome.isPalindrome(sentence)); + assertTrue(palindrome.isPalindrome(sentence)); } @Test public void whenReverseWord_shouldBePalindrome() { for(String word:words) - Assert.assertTrue(palindrome.isPalindromeReverseTheString(word)); + assertTrue(palindrome.isPalindromeReverseTheString(word)); } @Test public void whenReverseSentence_shouldBePalindrome() { for(String sentence:sentences) - Assert.assertTrue(palindrome.isPalindromeReverseTheString(sentence)); + assertTrue(palindrome.isPalindromeReverseTheString(sentence)); } @Test public void whenStringBuilderWord_shouldBePalindrome() { for(String word:words) - Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(word)); + assertTrue(palindrome.isPalindromeUsingStringBuilder(word)); } @Test public void whenStringBuilderSentence_shouldBePalindrome() { for(String sentence:sentences) - Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence)); + assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence)); } @Test public void whenStringBufferWord_shouldBePalindrome() { for(String word:words) - Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(word)); + assertTrue(palindrome.isPalindromeUsingStringBuffer(word)); } @Test public void whenStringBufferSentence_shouldBePalindrome() { for(String sentence:sentences) - Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence)); + assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence)); } @Test @@ -80,7 +80,7 @@ public class WhenCheckingPalindrome { word = word.replaceAll("\\s+", "").toLowerCase(); int backward = word.length()-1; - Assert.assertTrue(palindrome.isPalindromeRecursive(word,0,backward)); + assertTrue(palindrome.isPalindromeRecursive(word,0,backward)); } } @@ -90,25 +90,21 @@ public class WhenCheckingPalindrome { sentence = sentence.replaceAll("\\s+", "").toLowerCase(); int backward = sentence.length()-1; - Assert.assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward)); + assertTrue(palindrome.isPalindromeRecursive(sentence,0,backward)); } } @Test public void whenPalindromeStreams_wordShouldBePalindrome() { - String[] expected = Arrays.stream(words) - .filter(palindrome::isPalindrome) - .toArray(String[]::new); - - Assert.assertArrayEquals(expected, words); + for(String word:words) { + assertTrue(palindrome.isPalindromeUsingIntStream(word)); + } } @Test public void whenPalindromeStreams_sentenceShouldBePalindrome() { - String[] expected = Arrays.stream(sentences) - .filter(palindrome::isPalindrome) - .toArray(String[]::new); - - Assert.assertArrayEquals(expected, sentences); + for(String sentence:sentences) { + assertTrue(palindrome.isPalindromeUsingIntStream(sentence)); + } } } From 4c044fcaeec33a3b68637f751672a8f48de74c83 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 10 Feb 2018 17:45:01 +0200 Subject: [PATCH 2/3] remove imports --- core-java/src/test/java/com/baeldung/string/PalindromeTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/string/PalindromeTest.java b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java index ef78a2d840..d1a05b6617 100644 --- a/core-java/src/test/java/com/baeldung/string/PalindromeTest.java +++ b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java @@ -1,7 +1,5 @@ package com.baeldung.string; -import java.util.Arrays; - import static org.junit.Assert.*; import org.junit.Test; From 7c6a4b0b4dc5250faf6b17cac8a69fece05f58d1 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sat, 10 Feb 2018 18:48:41 +0200 Subject: [PATCH 3/3] Update Palindrome.java --- core-java/src/main/java/com/baeldung/string/Palindrome.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java/src/main/java/com/baeldung/string/Palindrome.java b/core-java/src/main/java/com/baeldung/string/Palindrome.java index d70a89525a..0759787249 100644 --- a/core-java/src/main/java/com/baeldung/string/Palindrome.java +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -58,4 +58,5 @@ public class Palindrome { return IntStream.range(0, temp.length() / 2) .noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1)); } + }