Java 遍历hashmaps并访问其元素
我想遍历hashmap。我知道我可以很容易地使用入口集。但问题是我想同时访问两个元素 例如:Java 遍历hashmaps并访问其元素,java,iterator,hashmap,Java,Iterator,Hashmap,我想遍历hashmap。我知道我可以很容易地使用入口集。但问题是我想同时访问两个元素 例如: HasHMap<Integer,Point> myMap = new HashMap<Integer,Point>(); //I add some points to the map where integer is the id of that point 我希望能够同时访问两个元素,以便使用图形绘制线方法 我不确定是否有办法 注意:我使用hashmap是
HasHMap<Integer,Point> myMap = new HashMap<Integer,Point>();
//I add some points to the map where integer is the id of that point
我希望能够同时访问两个元素,以便使用图形绘制线方法
我不确定是否有办法
注意:我使用hashmap是因为它很容易通过id找到任何点,因为我的地图的多边形是由id列表组成的。只要有一个单独的map.Entry变量,您可以在循环中设置它。然后您可以使用drawTo来使用这个变量和循环变量
Map.Entry<?, ?> oldValue = null;
for(Map.Entry<?, ?> newValue : values) {
if (oldValue != null) {
doSomethingWithBoth(oldValue, newValue);
}
oldValue = newValue;
}
我不知道你在用什么样的变量,所以?。。。。您应该在那里输入您正在使用的类型。每对两点是否可能重复?或者只是两个特定的点?您可以多次调用myMap.get.yes,遍历每对两点@路易斯瓦瑟曼:这很好用。虽然我花了一点时间才理解它的逻辑。谢谢这项工作背后的伟大逻辑。