2017-10-05 4 views
2

Ich bin in das folgende Problem fest:Verwendung von Guave Sets.cartesianProduct mit unbekannten Anzahl von Argumenten

ich eine dynamische Liste der Elemente haben wie folgt definiert:

List<Object> itemList = ArrayList(ArrayList<Object1, Object2, Double[]>) 

Die Object1 und Object2 sind hier nicht von Interesse, aber das Array Double[], das eine beliebige Anzahl von Einträgen enthält.

In meinem Code iteriere ich über die äußere ArrayList und versuche, das kartesische Produkt von Guava zu berechnen. Bis jetzt habe ich so etwas wie (teilweise nicht funktionierenden Code, sorry ...):

private Set<List<Set<Double>>> getValueCombinations() { 
    List<Set<Double>> valuesOfInnerArrays = new ArrayList<>(); 
    // Loop over the list of device data sets in the class and add the trim value vectors to a list for further 
    // processing and cartesian prooduct generation. 
    for (Integer indexCounter = 0; indexCounter < OuterArrayList.size(); indexCounter++) { 
     final List<Object> innerDataSet = (List<Object>) OuterArrayList.get(indexCounter); 
     final Set<Double> innerDoubleArray = ImmutableSet.of(((List<Double>) innerDataSet.get(2)).toArray(new Double[])); 
     valuesOfInnerArrays.add(innerDoubleArray); 
    } 
    ImmutableList<Set<Double>> test = ImmutableList.of(valuesOfInnerArrays) 
    // generate Cartesian product of all trim vectors = a n x m matrix of all combinations of settings 
    final Set<List<Set<Double>>> cartesianProduct = Sets.cartesianProduct(ImmutableList.of(valuesOfInnerArrays)); 

    return cartesianProduct; 
} 

In allen Beispielen fand ich, nennen sie immer Kartesisches Produkt mit bekannten Sets, die ich nicht tun kann:

Set<Double> first = ImmutableSet.of(1., 2.); 
Set<Double> second = ImmutableSet.of(3., 4.); 
Set<List<Double>> result = 
Sets.cartesianProduct(ImmutableList.of(first, second)); 

Was ich am Ende haben möchte, sind alle Kombinationen der Zahlen, die in den inneren Double [] - Arrays gespeichert sind.

Jede Hilfe wird geschätzt.

Antwort

2

Dank the Post "Java Guava CartesianProduct" habe ich mein Problem gelöst. Meine letzte Lösung sieht wie folgt aus:

private Set<List<Double>> getValueCombinations() { 
    final List<Set<Double>> valuesOfInnerArrays = new ArrayList<>(); 
    // Loop over the list of device data sets in the class and add the value vectors to a list for further 
    // processing and cartesian product generation. 
    for (Integer indexCounter = 0; indexCounter < outerArrayList.size(); indexCounter++) { 
     final List<Object> innerDataSet = (List<Object>) deviceDataSets.get(indexCounter); 
     final SortedSet<Double> >innerDoubleArray = new TreeSet<>((List<Double>) innerDataSet.get(2)); 
     valuesOfInnerArrays.add(innerDoubleArray); 
    } 

    return Sets.cartesianProduct(valuesOfInnerArrays); 
} 

Zusätzlich ich das Format meiner Eingabeliste geändert:

List<Object> itemList = ArrayList(ArrayList<Object1, Object2, List<Double>>) 
Verwandte Themen