I want to iterate through each key of a hashmap and compare it to each key below it (so I don't compare keys twice). I want to see which slopes are perpendicular to each other. If the product of the slopes is -1, they are and I will do something with the arraylists with those keys.
So far I have built the map using:
Map<Double, List<Double>> map = new HashMap<Double, List<Double>>();
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(line[4][i] == line[4][j]) {
double distance = 7.0; //distance();
List<Double> array = (map.containsKey(line[4][i])) ? map.get(line[4][i]):new ArrayList<Double>();
array.add(distance);
map.put(line[4][i], array);
// System.out.println(Arrays.asList(array));
// System.out.println(distance);
}
}
}
And attempt to iterate through the map with:
Iterator<Entry<Double, List<Double>>> i = map.entrySet().iterator();
while(i.hasNext()) {
Entry next = i.next();
i.remove();
for(Entry e : map.entrySet()) {
System.out.println(next + " " + e);
//ERROR: The method parseDouble(String) in the type Double is not applicable for the arguments (Object)
if((Double.parseDouble(next.getKey()) * (Double.parseDouble(e.getKey()))) == -1)
System.out.println("pair"); //do something
}
}
But I can't parse an object to a double, and I'm lost as to how to continue. Any help is appreciated
to your question, use type qualifiers for the next Entry:
Map.Entry<Double, List<Double>> next = i.next();
and here too:
Map.Entry<Double, List<Double>> e : map.entrySet()
then the compiler will know that getKey() will return Doubles, and allow to multiply them with each other.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.