I have a class Product
where a product has a name:
package main;
public class Product {
private String name;
public Product(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
And a class Combination
where a combination has an array list of products:
package main;
import java.util.ArrayList;
public class Combination {
private ArrayList<Product> products;
public Combination(ArrayList<Product> products) {
super();
this.products = products;
}
public ArrayList<Product> getProducts() {
return products;
}
public void setProducts(ArrayList<Product> products) {
this.products = products;
}
}
In Main
I create an array list of products and I want to get all possible combinations of products.
package main;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Product> products = new ArrayList<Product>();
products.add(new Product("p1"));
products.add(new Product("p2"));
products.add(new Product("p3"));
products.add(new Product("p4"));
ArrayList<Combination> combinations = getCombinations(products);
}
public static ArrayList<Combination> getCombinations(ArrayList<Product> products){
ArrayList<Combination> combinations = new ArrayList<Combination>();
//CODE TO ADD
return combinations;
}
}
What is the fastest solution to get all combinations? In the shown example I will get the following combinations:
p1
p1 p2
p1 p2 p3
p1 p2 p3 p4
p1 p2 p4
p1 p3
p1 p3 p4
p1 p4
p2
p2 p3
p2 p3 p4
p2 p4
p3
p3 p4
p4
I don't care about the order in which I retrieve the combinations, the important is to get all of them in the fastest way.