diff --git a/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/cartesianproduct/CartesianProduct.java b/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/cartesianproduct/CartesianProduct.java index 23d2c7aee2..d4725d74f1 100644 --- a/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/cartesianproduct/CartesianProduct.java +++ b/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/cartesianproduct/CartesianProduct.java @@ -9,7 +9,6 @@ import java.util.stream.Stream; import com.google.common.collect.Sets; public class CartesianProduct { - // Responsible for calculating Cartesian Product using iterative approach. public List> getCartesianProductIterative(List> sets) { List> result = new ArrayList<>(); if(sets == null || sets.isEmpty()) { @@ -31,14 +30,12 @@ public class CartesianProduct { return result; } - // Responsible for calculating Cartesian Product using recursive approach. public List> getCartesianProductRecursive(List> sets) { List> result = new ArrayList<>(); getCartesianProductRecursiveHelper(sets, 0, new ArrayList<>(), result); return result; } - // Helper method for recursive approach private void getCartesianProductRecursiveHelper(List> sets, int index, List current, List> result) { if(index == sets.size()) { result.add(new ArrayList<>(current)); @@ -52,12 +49,10 @@ public class CartesianProduct { } } - // Responsible for calculating Cartesian Product using streams. public List> getCartesianProductUsingStreams(List> sets) { return cartesianProduct(sets,0).collect(Collectors.toList()); } - // Helper method for streams approach. public Stream> cartesianProduct(List> sets, int index) { if(index == sets.size()) { List emptyList = new ArrayList<>(); @@ -71,7 +66,6 @@ public class CartesianProduct { })); } - // Responsible for calculating Cartesian Product using Guava library. public List> getCartesianProductUsingGuava(List> sets) { Set> cartesianProduct = Sets.cartesianProduct(sets); List> cartesianList = new ArrayList<>(cartesianProduct); diff --git a/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/cartesianproduct/CartesianProductUnitTest.java b/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/cartesianproduct/CartesianProductUnitTest.java index 726934e78a..cc9c01fb5f 100644 --- a/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/cartesianproduct/CartesianProductUnitTest.java +++ b/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/cartesianproduct/CartesianProductUnitTest.java @@ -19,7 +19,7 @@ public class CartesianProductUnitTest { ); @Test - public void shouldCalculateCartesianProductUsingStreams() { + public void whenUsingStreams_thenCalculateCartesianProduct() { List> expected = Arrays.asList( Arrays.asList(10, "John", 'I'), Arrays.asList(10, "John", 'J'), @@ -30,13 +30,13 @@ public class CartesianProductUnitTest { Arrays.asList(20, "Jack", 'I'), Arrays.asList(20, "Jack", 'J') ); - List> cartesianProduct = cp.getCartesianProductUsingStreams(sets); + assertEquals(expected, cartesianProduct); } @Test - public void shouldCalculateCartesianProductUsingRecursion() { + public void whenUsingRecursion_thenCalculateCartesianProduct() { List> expected = Arrays.asList( Arrays.asList(10, "John", 'I'), Arrays.asList(10, "John", 'J'), @@ -47,13 +47,13 @@ public class CartesianProductUnitTest { Arrays.asList(20, "Jack", 'I'), Arrays.asList(20, "Jack", 'J') ); - List> cartesianProduct = cp.getCartesianProductRecursive(sets); + assertEquals(expected, cartesianProduct); } @Test - public void shouldCalculateCartesianProductUsingIterativeApproach() { + public void whenUsingIterativeApproach_thenCalculateCartesianProduct() { List> expected = Arrays.asList( Arrays.asList(20, "Jack", 'J'), Arrays.asList(10, "Jack", 'J'), @@ -64,13 +64,13 @@ public class CartesianProductUnitTest { Arrays.asList(20, "John", 'I'), Arrays.asList(10, "John", 'I') ); - List> cartesianProduct = cp.getCartesianProductIterative(sets); + assertEquals(expected, cartesianProduct); } @Test - public void shouldCalculateCartesianProductUsingGuava() { + public void whenUsingGuava_thenCalculateCartesianProduct() { List> sets = new ArrayList<>(); sets.add(new HashSet<>(Arrays.asList(10, 20))); sets.add(new HashSet<>(Arrays.asList("John", "Jack"))); @@ -86,8 +86,8 @@ public class CartesianProductUnitTest { Arrays.asList(10, "Jack", 'I'), Arrays.asList(10, "Jack", 'J') ); - List> cartesianProduct = cp.getCartesianProductUsingGuava(sets); + assertEquals(expected, cartesianProduct); } } \ No newline at end of file