[英]Iterating through hashmap keys and comparing
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.
如果斜率的乘积为-1,则它们为,我将使用这些键对arraylist进行处理。
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. 但是我无法将一个对象解析为一个double,而且我对如何继续不知所措。 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. 那么编译器将知道getKey()将返回Doubles,并允许将它们彼此相乘。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.