BAEL 2330

* Implementing Hexagonal Architecture in java

* Removing duplicates from a string

* Fix for the code review feedback
- removing the hexagonal architecture code
- removing the methods removeDuplicatesUsingCharArray
- adding some meaningful sentences to test

* Fix for the code review feedback
- fix for removeDuplicatesUsingCharArray
- adding unit testing
- adding brackets around for loops

* Fix for the code review feedback

* Adding remove duplicates using java 8 distinct
adding assets to the unittests to test for empty strings

* Checking for empty strings in unit tests
Adding an if statement to check whether an empty string is passed to removeDuplicatesUsingSorting method
This commit is contained in:
kyleandari 2018-11-23 10:46:20 -05:00 committed by Emily Cheyne
parent 9127733ad1
commit 58a536c395
2 changed files with 40 additions and 9 deletions

View File

@ -44,16 +44,16 @@ public class RemoveDuplicateFromString {
}
String removeDuplicatesUsingSorting(String str) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
StringBuilder sb = new StringBuilder();
sb.append(chars[0]);
for (int i = 1; i < chars.length; i++) {
if (chars[i] != chars[i - 1]) {
sb.append(chars[i]);
if(!str.isEmpty()) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
sb.append(chars[0]);
for (int i = 1; i < chars.length; i++) {
if (chars[i] != chars[i - 1]) {
sb.append(chars[i]);
}
}
}
@ -90,6 +90,13 @@ public class RemoveDuplicateFromString {
return sb.toString();
}
String removeDuplicatesUsingDistinct(String str) {
StringBuilder sb = new StringBuilder();
str.chars().distinct().forEach(c -> sb.append((char) c));
return sb.toString();
}
}

View File

@ -8,6 +8,8 @@ public class RemoveDuplicateFromStringUnitTest {
private final static String STR1 = "racecar";
private final static String STR2 = "J2ee programming";
private final static String STR_EMPTY = "";
private RemoveDuplicateFromString removeDuplicateFromString;
@Before
@ -20,6 +22,8 @@ public class RemoveDuplicateFromStringUnitTest {
public void whenUsingCharArray_DuplicatesShouldBeRemovedWithoutKeepingStringOrder() {
String str1 = removeDuplicateFromString.removeDuplicatesUsingCharArray(STR1);
String str2 = removeDuplicateFromString.removeDuplicatesUsingCharArray(STR2);
String strEmpty = removeDuplicateFromString.removeDuplicatesUsingCharArray(STR_EMPTY);
Assert.assertEquals("", strEmpty);
Assert.assertEquals("ecar", str1);
Assert.assertEquals("J2e poraming", str2);
}
@ -28,6 +32,9 @@ public class RemoveDuplicateFromStringUnitTest {
public void whenUsingLinkedHashSet_DuplicatesShouldBeRemovedAndItKeepStringOrder() {
String str1 = removeDuplicateFromString.removeDuplicatesUsinglinkedHashSet(STR1);
String str2 = removeDuplicateFromString.removeDuplicatesUsinglinkedHashSet(STR2);
String strEmpty = removeDuplicateFromString.removeDuplicatesUsinglinkedHashSet(STR_EMPTY);
Assert.assertEquals("", strEmpty);
Assert.assertEquals("race", str1);
Assert.assertEquals("J2e progamin", str2);
}
@ -36,6 +43,9 @@ public class RemoveDuplicateFromStringUnitTest {
public void whenUsingSorting_DuplicatesShouldBeRemovedWithoutKeepingStringOrder() {
String str1 = removeDuplicateFromString.removeDuplicatesUsingSorting(STR1);
String str2 = removeDuplicateFromString.removeDuplicatesUsingSorting(STR2);
String strEmpty = removeDuplicateFromString.removeDuplicatesUsingSorting(STR_EMPTY);
Assert.assertEquals("", strEmpty);
Assert.assertEquals("acer", str1);
Assert.assertEquals(" 2Jaegimnopr", str2);
}
@ -44,6 +54,8 @@ public class RemoveDuplicateFromStringUnitTest {
public void whenUsingHashSet_DuplicatesShouldBeRemovedWithoutKeepingStringOrder() {
String str1 = removeDuplicateFromString.removeDuplicatesUsingHashSet(STR1);
String str2 = removeDuplicateFromString.removeDuplicatesUsingHashSet(STR2);
String strEmpty = removeDuplicateFromString.removeDuplicatesUsingHashSet(STR_EMPTY);
Assert.assertEquals("", strEmpty);
Assert.assertEquals("arce", str1);
Assert.assertEquals(" pa2regiJmno", str2);
}
@ -52,7 +64,19 @@ public class RemoveDuplicateFromStringUnitTest {
public void whenUsingIndexOf_DuplicatesShouldBeRemovedWithoutKeepingStringOrder() {
String str1 = removeDuplicateFromString.removeDuplicatesUsingIndexOf(STR1);
String str2 = removeDuplicateFromString.removeDuplicatesUsingIndexOf(STR2);
String strEmpty = removeDuplicateFromString.removeDuplicatesUsingIndexOf(STR_EMPTY);
Assert.assertEquals("", strEmpty);
Assert.assertEquals("ecar", str1);
Assert.assertEquals("J2e poraming", str2);
}
@Test
public void whenUsingJava8_DuplicatesShouldBeRemovedAndItKeepStringOrder() {
String str1 = removeDuplicateFromString.removeDuplicatesUsingDistinct(STR1);
String str2 = removeDuplicateFromString.removeDuplicatesUsingDistinct(STR2);
String strEmpty = removeDuplicateFromString.removeDuplicatesUsingDistinct(STR_EMPTY);
Assert.assertEquals("", strEmpty);
Assert.assertEquals("race", str1);
Assert.assertEquals("J2e progamin", str2);
}
}