# Share My Java Solution using Set of Map

• ``````public class Solution {
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
Map<String[], Double> eqMap = new HashMap<>();
for (int i = 0; i < values.length; i++) {
eqMap.put(equations[i], values[i]);
}

Arrays.sort(equations, new Comparator<String[]>(){
@Override
public int compare(String[] s1, String[] s2) {
String s1_small, s1_large, s2_small, s2_large;
s1_small = s1[0].compareTo(s1[1]) > 0 ? s1[0] : s1[1];
s1_large = s1[0].compareTo(s1[1]) > 0 ? s1[1] : s1[0];
s2_small = s2[0].compareTo(s2[1]) > 0 ? s2[0] : s2[1];
s2_large = s2[0].compareTo(s2[1]) > 0 ? s2[1] : s2[0];
if (s1_small.compareTo(s2_small) == 0) {
return s1_large.compareTo(s2_large);
} else {
return s1_small.compareTo(s2_small);
}
}
});

Set<Map<String, Double>> valMapSet = new HashSet<>();
valMapSet.add(new HashMap<String, Double>());
for (String[] eq: equations) {
boolean findSet = false;
for (Map<String, Double> valMap: valMapSet) {
if (valMap.containsKey(eq[0])) {
valMap.put(eq[1], valMap.get(eq[0]) * eqMap.get(eq));
findSet = true;
break;
} else if (valMap.containsKey(eq[1])) {
valMap.put(eq[0], valMap.get(eq[1]) / eqMap.get(eq));
findSet = true;
break;
} else {
continue;
}
}
if (!findSet) {
Map<String, Double> newValMap = new HashMap<>();
newValMap.put(eq[0], 1.0d);
newValMap.put(eq[1], eqMap.get(eq));
valMapSet.add(newValMap);
}
}
double[] ret = new double[queries.length];
for (int i = 0; i < queries.length; i++) {
boolean found = false;
for (Map<String, Double> valMap: valMapSet) {
// System.out.println("Scanning map: " + valMap);
if (!(valMap.containsKey(queries[i][0]) && valMap.containsKey(queries[i][1]))) {
continue;
} else {
ret[i] = valMap.get(queries[i][1]) / valMap.get(queries[i][0]);
found = true;
break;
}
}
if (!found) {
ret[i] = -1.0d;
}
}
return ret;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.