i need to build a position manager class to tell me if a position is available.
so i tried this :
enter code here
public class PositionManager {
Hashtable currentPositions = new Hashtable();
void occupiedPosition(int x,int y){
this.currentPositions.put(new Integer("4"),new Integer("5"));
this.currentPositions.put(new Integer("1"),new Integer("5"));
this.currentPositions.put(new Integer("11"),new Integer("3"));
this.currentPositions.put(new Integer("42"),new Integer("55"));
this.currentPositions.put(new Integer("11"),new Integer("53"));
Set keys = this.currentPositions.keySet(); // The set of keys in the map.
Iterator keyIter = keys.iterator();
System.out.println("The map contains the following associations:");
while (keyIter.hasNext()) {
Object key = keyIter.next(); // Get the next key.
Object value = this.currentPositions.get(key); // Get the value for that key.
System.out.println( " (" + key + "," + value + ")" );
}
}
public static void main(String[] args) {
new PositionManager().occupiedPosition(3, 3);
}
}
of course this is just a test , what i am trying to do is retreiving all the positions that are used ,the problem is that i cant have duplicates of the keys.
so what kind of data structure should i use .
thanks in advance.
I’d approach this problem like this by just creating a set of positions. A set models a collection of objects that can only occur once. In comparison a map structure stores a set of key / value associations. From my reading of your question, I think a set structure makes most sense.
You need to implement hashCode() so that items can be distributed uniformly in the set and equals() so that objects can be compared. See here for more information.
Make sure you define equals / hashCode appropriately (there’s plenty of links for this)
You can now test whether a point is in the set using the contains method such as: