public void whenCalculatingCartesianProductOfSets_thenCorrect() {
Set<Character> first = ImmutableSet.of('a', 'b');
Set<Character> second = ImmutableSet.of('c', 'd');
Set<List<Character>> result =
Sets.cartesianProduct(ImmutableList.of(first, second));
Function<List<Character>, String> func =
new Function<List<Character>, String>() {
public String apply(List<Character> input) {
return Joiner.on(" ").join(input);
}
};
Iterable<String> joined = Iterables.transform(result, func);
assertThat(joined, containsInAnyOrder("a c", "a d", "b c", "b d"));
}
public static void main(String[] args) {
Set<String> animals = ImmutableSet.of("gerbil", "hamster");
Set<String> fruits = ImmutableSet.of("apple", "orange", "banana");
Set<List<String>> product = Sets.cartesianProduct(animals, fruits);
// {{"gerbil", "apple"}, {"gerbil", "orange"}, {"gerbil", "banana"},
// {"hamster", "apple"}, {"hamster", "orange"}, {"hamster", "banana"}}
}
public void cartesian_product_guava () {
Set<String> first = Sets.newHashSet("a", "b");
Set<String> second = Sets.newHashSet("c", "d");
Set<List<String>> cartesianProduct = Sets.cartesianProduct(first, second);
List<String> b_c = Lists.newArrayList("b", "c");
List<String> b_d = Lists.newArrayList("b", "d");
List<String> a_c = Lists.newArrayList("a", "c");
List<String> a_d = Lists.newArrayList("a", "d");
assertThat(cartesianProduct,
anyOf(containsInAnyOrder(b_c, b_d, a_c, a_d)));
}
public static void main(String[] args) {
List<Set<String>> interimList = new ArrayList<Set<String>>();
//converting List<List<String>> to List<Set<String>>
for (List<String> tmp : allLists) {
Set<String> interimSet = new HashSet<String>(tmp);
interimList.add(interimSet);
}
System.out.println(interimList);
Sets.cartesianProduct(interimList);
}
public void ensembles() {
Set<String> chiffres = Sets.newHashSet("1", "2", "3");
Set<String> lettres = Sets.newHashSet("A", "B", "C");
Set<String> voyelles= Sets.newHashSet("A", "E", "I", "O", "U", "Y");
// Cartesian Product
Set<List<String>> combinations = Sets.cartesianProduct(chiffres, lettres);
assertEquals(9, combinations.size());
// Difference
Sets.SetView<String> difference = Sets.difference(lettres, voyelles);
assertEquals(2, difference.size());
assertTrue(difference.containsAll(Sets.newHashSet("B", "C")));
// Intersection
Sets.SetView<String> intersection = Sets.intersection(lettres, voyelles);
assertEquals(1, intersection.size());
assertEquals("A", intersection.iterator().next());
// Union
Sets.SetView<String> union = Sets.union(lettres, voyelles);
assertEquals(8, union.size());
assertTrue(union.containsAll(Sets.newHashSet("A", "B", "C", "E", "I", "O", "U", "Y")));
}
private Iterable<List<Object>> buildCartesianProduct(Set<?>... sets) {
List<Set<Optional<?>>> optionalSets = Lists.newArrayListWithExpectedSize(sets.length);
for (Set<?> set : sets) {
Set<Optional<?>> optionalSet =
Sets.newLinkedHashSet(Iterables.transform(set, NULLABLE_TO_OPTIONAL));
optionalSets.add(optionalSet);
}
Set<List<Optional<?>>> cartesianProduct = Sets.cartesianProduct(optionalSets);
return Iterables.transform(cartesianProduct,
new Function<List<Optional<?>>, List<Object>>() {
@Override public List<Object> apply(List<Optional<?>> objs) {
return Lists.transform(objs, OPTIONAL_TO_NULLABLE);
}
});
}
public void testCartesianProductTooBig() {
Set<Integer> set = ContiguousSet.create(Range.closed(0, 10000), DiscreteDomain.integers());
try {
Sets.cartesianProduct(set, set, set, set, set);
fail("Expected IAE");
} catch (IllegalArgumentException expected) {
}
}
private Iterable<List<Object>> buildCartesianProduct(Set<?>... sets) {
List<Set<Optional<?>>> optionalSets = Lists.newArrayListWithExpectedSize(sets.length);
for (Set<?> set : sets) {
Set<Optional<?>> optionalSet =
Sets.newLinkedHashSet(Iterables.transform(set, NULLABLE_TO_OPTIONAL));
optionalSets.add(optionalSet);
}
Set<List<Optional<?>>> cartesianProduct = Sets.cartesianProduct(optionalSets);
return Iterables.transform(
cartesianProduct,
new Function<List<Optional<?>>, List<Object>>() {
@Override
public List<Object> apply(List<Optional<?>> objs) {
return Lists.transform(objs, OPTIONAL_TO_NULLABLE);
}
});
}
public RoutingResult route() {
RoutingResult result = new RoutingResult();
for (Entry<String, Set<String>> entry : getDataSourceLogicTablesMap().entrySet()) {
List<Set<String>> actualTableGroups = getActualTableGroups(entry.getKey(), entry.getValue());
List<Set<RoutingTable>> routingTableGroups = toRoutingTableGroups(entry.getKey(), actualTableGroups);
result.getTableUnits().getTableUnits().addAll(getTableUnits(entry.getKey(), Sets.cartesianProduct(routingTableGroups)).getTableUnits());
}
return result;
}
public static Iterable<Object[]> compressionStrategiesAndByteOrders()
{
Set<List<Object>> combinations = Sets.cartesianProduct(
Sets.newHashSet(CompressionStrategy.noNoneValues()),
Sets.newHashSet(ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN)
);
return Iterables.transform(
combinations,
(Function<List, Object[]>) input -> new Object[]{input.get(0), input.get(1)}
);
}