Given the following code :
public class Game {
private Map<String,Coordinate> m_myHashMap;
... // more code
}
public Game(Display display,int level)
{
this.m_myHashMap = new HashMap<String,Coordinate>();
... // more code
}
class Coordinate
package model;
public class Coordinate {
private int xCoordinate;
private int yCoordinate;
private int sign;
public Coordinate(int x,int y)
{
this.xCoordinate = x;
this.yCoordinate = y;
this.sign = 0;
}
public int getXCoordinate()
{
return this.xCoordinate;
}
public int getYCoordinate()
{
return this.yCoordinate;
}
public void setSign(int number)
{
this.sign = number;
}
public int getSign()
{
return this.sign;
}
public void setXcoordinate(int newX)
{
this.xCoordinate = newX;
}
public void setYcoordinate(int newY)
{
this.yCoordinate = newY;
}
}
And a method of Game class :
private void placeTreasuresInMaze(PaintEvent e)
{
// e.gc.drawPolygon(new int[] { 25+x,5+y,45+x,45+y,5+x,45+y });
int numberOfTreasures = this.m_numberOfPlayers * 3; // calculate number of treasures
Set set = this.m_myHashMap.entrySet();
Iterator iterator = set.iterator();
while (numberOfTreasures > 0 && iterator.hasNext())
{
numberOfTreasures--;
// need to add more code here
}
}
HashMap doesn’t have an iterator , so I used Set in order to get the elements of the hashmap.
My problem started when I wanted to iterate on the values of the HashMap , but since that’s not possible , I tried with Set , but Set returns an Object and not the Coordinate object itself .
Is there a way to get it ?
Regards,Ron
The problem is that you’re using the raw types for the set and iterator – you should use the generic types:
Or:
On the other hand, if you really do just want to iterate over the values of the map: