Cartesian Products with sets in java

走远了吗. 提交于 2019-12-11 18:55:03

问题


I am trying to create a cartesian product method in java that accepts sets as arguments and returns a set pair. The code I have coverts the argumented sets to arrays and then does the cartesian product but i can't add it back to the set pair that i want to return. Is there an easier way to do this? Thanks in advance.

public static <S, T> Set<Pair<S, T>> cartesianProduct(Set<S> a, Set<T> b) {
    Set<Pair<S, T>> product = new HashSet<Pair<S, T>>();


    String[] arrayA = new String[100];
    String[] arrayB= new String[100];

    a.toArray(arrayA);
    b.toArray(arrayB);

    for(int i = 0; i < a.size(); i++){
        for(int j = 0; j < b.size(); j++){
            product.add(arrayA[i],arrayB[j]);
        }
    }
    return product;
}

回答1:


this looks simpler,

public static <S, T> Set<Pair<S, T>> cartesianProduct(Set<S> a, Set<T> b) {
    Set<Pair<S, T>> product = new HashSet<Pair<S, T>>();

    for(S s : a) {
        for(T t : b) {
            product.add(new ImmutablePair<S, T>(s,t));
        }
    }

    return product;
}



回答2:


Assuming you're using Pair from Apache Commons, then I think you want the add to be

product.add(Pair.of(arrayA[i],arrayB[j]));

There's no add method for sets that takes two arguments. You have to create the Pair to add to the set. If that doesn't compile, try

product.add(Pair<S,T>.of(arrayA[i],arrayB[j]));

Also, I'm assuming you meant S and T for your arrays, instead of String. There's no reason to preallocate a certain number of elements. Furthermore, the way you've written it, if there are more than 100 elements in either set, toArray will return an all new array with the desired size, but you're not using the function result so that array will be lost. I'd prefer:

S[] arrayA = a.toArray(new S[0]);
T[] arrayB = b.toArray(new T[0]);

The zero-length arrays are just "dummies" whose purpose is to get toArray to return arrays with the correct element type, instead of Object[].

EDIT: Using an enhanced for loop is a lot better than using arrays. See Camilo's answer.



来源:https://stackoverflow.com/questions/21640274/cartesian-products-with-sets-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!