[BAEL-6708] Address review comments
This commit is contained in:
parent
2337e29d0b
commit
de16d8c39c
|
@ -9,7 +9,6 @@ import java.util.stream.Stream;
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
|
|
||||||
public class CartesianProduct {
|
public class CartesianProduct {
|
||||||
// Responsible for calculating Cartesian Product using iterative approach.
|
|
||||||
public List<List<Object>> getCartesianProductIterative(List<List<Object>> sets) {
|
public List<List<Object>> getCartesianProductIterative(List<List<Object>> sets) {
|
||||||
List<List<Object>> result = new ArrayList<>();
|
List<List<Object>> result = new ArrayList<>();
|
||||||
if(sets == null || sets.isEmpty()) {
|
if(sets == null || sets.isEmpty()) {
|
||||||
|
@ -31,14 +30,12 @@ public class CartesianProduct {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Responsible for calculating Cartesian Product using recursive approach.
|
|
||||||
public List<List<Object>> getCartesianProductRecursive(List<List<Object>> sets) {
|
public List<List<Object>> getCartesianProductRecursive(List<List<Object>> sets) {
|
||||||
List<List<Object>> result = new ArrayList<>();
|
List<List<Object>> result = new ArrayList<>();
|
||||||
getCartesianProductRecursiveHelper(sets, 0, new ArrayList<>(), result);
|
getCartesianProductRecursiveHelper(sets, 0, new ArrayList<>(), result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper method for recursive approach
|
|
||||||
private void getCartesianProductRecursiveHelper(List<List<Object>> sets, int index, List<Object> current, List<List<Object>> result) {
|
private void getCartesianProductRecursiveHelper(List<List<Object>> sets, int index, List<Object> current, List<List<Object>> result) {
|
||||||
if(index == sets.size()) {
|
if(index == sets.size()) {
|
||||||
result.add(new ArrayList<>(current));
|
result.add(new ArrayList<>(current));
|
||||||
|
@ -52,12 +49,10 @@ public class CartesianProduct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Responsible for calculating Cartesian Product using streams.
|
|
||||||
public List<List<Object>> getCartesianProductUsingStreams(List<List<Object>> sets) {
|
public List<List<Object>> getCartesianProductUsingStreams(List<List<Object>> sets) {
|
||||||
return cartesianProduct(sets,0).collect(Collectors.toList());
|
return cartesianProduct(sets,0).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper method for streams approach.
|
|
||||||
public Stream<List<Object>> cartesianProduct(List<List<Object>> sets, int index) {
|
public Stream<List<Object>> cartesianProduct(List<List<Object>> sets, int index) {
|
||||||
if(index == sets.size()) {
|
if(index == sets.size()) {
|
||||||
List<Object> emptyList = new ArrayList<>();
|
List<Object> emptyList = new ArrayList<>();
|
||||||
|
@ -71,7 +66,6 @@ public class CartesianProduct {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Responsible for calculating Cartesian Product using Guava library.
|
|
||||||
public List<List<Object>> getCartesianProductUsingGuava(List<Set<Object>> sets) {
|
public List<List<Object>> getCartesianProductUsingGuava(List<Set<Object>> sets) {
|
||||||
Set<List<Object>> cartesianProduct = Sets.cartesianProduct(sets);
|
Set<List<Object>> cartesianProduct = Sets.cartesianProduct(sets);
|
||||||
List<List<Object>> cartesianList = new ArrayList<>(cartesianProduct);
|
List<List<Object>> cartesianList = new ArrayList<>(cartesianProduct);
|
||||||
|
|
|
@ -19,7 +19,7 @@ public class CartesianProductUnitTest {
|
||||||
);
|
);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCalculateCartesianProductUsingStreams() {
|
public void whenUsingStreams_thenCalculateCartesianProduct() {
|
||||||
List<List<Object>> expected = Arrays.asList(
|
List<List<Object>> expected = Arrays.asList(
|
||||||
Arrays.asList(10, "John", 'I'),
|
Arrays.asList(10, "John", 'I'),
|
||||||
Arrays.asList(10, "John", 'J'),
|
Arrays.asList(10, "John", 'J'),
|
||||||
|
@ -30,13 +30,13 @@ public class CartesianProductUnitTest {
|
||||||
Arrays.asList(20, "Jack", 'I'),
|
Arrays.asList(20, "Jack", 'I'),
|
||||||
Arrays.asList(20, "Jack", 'J')
|
Arrays.asList(20, "Jack", 'J')
|
||||||
);
|
);
|
||||||
|
|
||||||
List<List<Object>> cartesianProduct = cp.getCartesianProductUsingStreams(sets);
|
List<List<Object>> cartesianProduct = cp.getCartesianProductUsingStreams(sets);
|
||||||
|
|
||||||
assertEquals(expected, cartesianProduct);
|
assertEquals(expected, cartesianProduct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCalculateCartesianProductUsingRecursion() {
|
public void whenUsingRecursion_thenCalculateCartesianProduct() {
|
||||||
List<List<Object>> expected = Arrays.asList(
|
List<List<Object>> expected = Arrays.asList(
|
||||||
Arrays.asList(10, "John", 'I'),
|
Arrays.asList(10, "John", 'I'),
|
||||||
Arrays.asList(10, "John", 'J'),
|
Arrays.asList(10, "John", 'J'),
|
||||||
|
@ -47,13 +47,13 @@ public class CartesianProductUnitTest {
|
||||||
Arrays.asList(20, "Jack", 'I'),
|
Arrays.asList(20, "Jack", 'I'),
|
||||||
Arrays.asList(20, "Jack", 'J')
|
Arrays.asList(20, "Jack", 'J')
|
||||||
);
|
);
|
||||||
|
|
||||||
List<List<Object>> cartesianProduct = cp.getCartesianProductRecursive(sets);
|
List<List<Object>> cartesianProduct = cp.getCartesianProductRecursive(sets);
|
||||||
|
|
||||||
assertEquals(expected, cartesianProduct);
|
assertEquals(expected, cartesianProduct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCalculateCartesianProductUsingIterativeApproach() {
|
public void whenUsingIterativeApproach_thenCalculateCartesianProduct() {
|
||||||
List<List<Object>> expected = Arrays.asList(
|
List<List<Object>> expected = Arrays.asList(
|
||||||
Arrays.asList(20, "Jack", 'J'),
|
Arrays.asList(20, "Jack", 'J'),
|
||||||
Arrays.asList(10, "Jack", 'J'),
|
Arrays.asList(10, "Jack", 'J'),
|
||||||
|
@ -64,13 +64,13 @@ public class CartesianProductUnitTest {
|
||||||
Arrays.asList(20, "John", 'I'),
|
Arrays.asList(20, "John", 'I'),
|
||||||
Arrays.asList(10, "John", 'I')
|
Arrays.asList(10, "John", 'I')
|
||||||
);
|
);
|
||||||
|
|
||||||
List<List<Object>> cartesianProduct = cp.getCartesianProductIterative(sets);
|
List<List<Object>> cartesianProduct = cp.getCartesianProductIterative(sets);
|
||||||
|
|
||||||
assertEquals(expected, cartesianProduct);
|
assertEquals(expected, cartesianProduct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCalculateCartesianProductUsingGuava() {
|
public void whenUsingGuava_thenCalculateCartesianProduct() {
|
||||||
List<Set<Object>> sets = new ArrayList<>();
|
List<Set<Object>> sets = new ArrayList<>();
|
||||||
sets.add(new HashSet<>(Arrays.asList(10, 20)));
|
sets.add(new HashSet<>(Arrays.asList(10, 20)));
|
||||||
sets.add(new HashSet<>(Arrays.asList("John", "Jack")));
|
sets.add(new HashSet<>(Arrays.asList("John", "Jack")));
|
||||||
|
@ -86,8 +86,8 @@ public class CartesianProductUnitTest {
|
||||||
Arrays.asList(10, "Jack", 'I'),
|
Arrays.asList(10, "Jack", 'I'),
|
||||||
Arrays.asList(10, "Jack", 'J')
|
Arrays.asList(10, "Jack", 'J')
|
||||||
);
|
);
|
||||||
|
|
||||||
List<List<Object>> cartesianProduct = cp.getCartesianProductUsingGuava(sets);
|
List<List<Object>> cartesianProduct = cp.getCartesianProductUsingGuava(sets);
|
||||||
|
|
||||||
assertEquals(expected, cartesianProduct);
|
assertEquals(expected, cartesianProduct);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue