diff --git a/core-java-collections/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java b/core-java-collections/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java index 3123295641..5c9464182e 100644 --- a/core-java-collections/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java +++ b/core-java-collections/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java @@ -45,8 +45,8 @@ public class Foo { } public Foo deepCopy() { - return new Foo(this.id, this.name, - this.parent != null ? this.parent.deepCopy() : null); + return new Foo( + this.id, this.name, this.parent != null ? this.parent.deepCopy() : null); } } diff --git a/core-java-collections/src/test/java/com/baeldung/convertcollectiontoarraylist/FooUnitTest.java b/core-java-collections/src/test/java/com/baeldung/convertcollectiontoarraylist/FooUnitTest.java index 5b46434a0b..2b5751ef9e 100644 --- a/core-java-collections/src/test/java/com/baeldung/convertcollectiontoarraylist/FooUnitTest.java +++ b/core-java-collections/src/test/java/com/baeldung/convertcollectiontoarraylist/FooUnitTest.java @@ -33,6 +33,9 @@ public class FooUnitTest { srcCollection.add(sam); srcCollection.add(alice); srcCollection.add(buffy); + + // make sure the collection isn't sorted accidentally + assertFalse("Oops: source collection is already sorted!", isSorted(srcCollection)); } /** @@ -50,6 +53,7 @@ public class FooUnitTest { @Test public void whenUsingStream_thenVerifyShallowCopy() { ArrayList newList = srcCollection.stream().collect(toCollection(ArrayList::new)); + verifyShallowCopy(srcCollection, newList); } @@ -61,6 +65,7 @@ public class FooUnitTest { ArrayList newList = srcCollection.stream() .map(foo -> foo.deepCopy()) .collect(toCollection(ArrayList::new)); + verifyDeepCopy(srcCollection, newList); } @@ -69,11 +74,11 @@ public class FooUnitTest { */ @Test public void whenUsingSortedStream_thenVerifySortOrder() { - assertFalse("Oops: source collection is already sorted!", isSorted(srcCollection)); ArrayList newList = srcCollection.stream() .map(foo -> foo.deepCopy()) .sorted(Comparator.comparing(Foo::getName)) .collect(toCollection(ArrayList::new)); + assertTrue("ArrayList is not sorted by name", isSorted(newList)); } @@ -130,7 +135,7 @@ public class FooUnitTest { * @param c collection of Foo * @return true if the collection is sorted by name */ - private boolean isSorted(Collection c) { + private static boolean isSorted(Collection c) { String prevName = null; for (Foo foo : c) { if (prevName == null || foo.getName().compareTo(prevName) > 0) {