I am looking for a Java library that can produce combinations of vectors like this:
Given:
vector1 = {A, B, C}
vector2 = {0, 1, 2}
Produce the following combinations:
A, 0
A, 1
A, 2
B, 0
B, 1
B, 2
C, 0
C, 1
C, 2
The number of vectors give the number of dimensions (columns of the combinations).
In Python the function product from the intertools library does exactly this, but I haven't seen any Java library to do it.
Thanks.
You can use java8 streams to do it almost as simply as if you called a function from a library. Assuming you already have:
List<String> vector1 = Arrays.asList("A","B","C");
List<Integer> vector2 = Arrays.asList(1,2,3);
You can get your expected result in a following way
Map<String, Integer> result = new HashMap<>();
vector1.stream().forEach(o1 -> vector2.stream().forEach(o2 -> result.put(o1,o2)));
Or if you prefer List of Tuples, then you need to either create a class for your pairs, or use a Tuple
Solution #1 : You can use a map to associate a string with a list of integers.
public static void main(String[] args) {
List<String> v1 = Arrays.asList("A", "B", "C");
List<Integer> v2 = Arrays.asList(0, 1, 2);
Map<String, List<Integer>> product = getProduct(v1, v2);
}
public static Map<String, List<Integer>> getProduct(List<String> v1, List<Integer> v2) {
Map<String, List<Integer>> product = new HashMap<>();
for (String e1 : v1) {
product.put(e1, v2);
}
return product;
}
The data are represented this way :

Solution #2 : You create a list of Combination objects.
public class Combination<T1, T2> {
protected final T1 value1;
protected final T2 value2;
public Combination(T1 value1, T2 value2) {
this.value1 = value1;
this.value2 = value2;
}
public T1 getValue1() {
return value1;
}
public T2 getValue2() {
return value2;
}
}
public class CombinationGenerator<T1, T2> {
protected final List<T1> values1;
protected final List<T2> values2;
public CombinationGenerator(List<T1> values1, List<T2> values2) {
this.values1 = values1;
this.values2 = values2;
}
public List<Combination<T1, T2>> getCombinations() {
List<Combination<T1, T2>> combinations = new LinkedList<>();
for (T1 e1 : values1) {
for (T2 e2 : values2) {
combinations.add(new Combination<>(e1, e2));
}
}
return combinations;
}
}
public static void main(String[] args) {
List<String> v1 = Arrays.asList("A", "B", "C");
List<Integer> v2 = Arrays.asList(0, 1, 2);
CombinationGenerator<String, Integer> combGen = new CombinationGenerator<>(v1, v2);
List<Combination<String, Integer>> combinations = combGen.getCombinations();
}
This solution returns a list of 9 combinations :

Edit: For the solution #1, you can use Guava's Multimap
public static Multimap<String, Integer> getCombinations(List<String> v1, List<Integer> v2) {
Multimap<String, Integer> combinations = ArrayListMultimap.create();
for (String e1 : v1) {
for (Integer e2 : v2) {
combinations.put(e1, e2);
}
}
return combinations;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With