开发者

what kind of data structure do i need to implement a hashlist for storing coordinates?

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")开发者_JAVA百科);
    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 might just be able to use an existing Point depending on what you
// want to do with the position
class Position {
  int x;
  int y;

  // implementations of hashCode() + equals()
  }
}

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.

Set<Position> positions = new HashSet<Position>();
positions.add(new Position(3,4));
positions.add(new Position(5,6)); // and so on

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:

positions.contains(new Point(2,1)); // returns false
positions.contains(new Point(3,4)); // returns true


I would suggest using google-collection's MultiMap. This is effectively a managed type for Map<K, Collection<V>>.

Also of interest may be the Multimaps class, which gives you Multimap<K,V> invertFrom(Multimap<V,K>)

Then you could end up with:

public boolean isPositionOccupied(int x, int y) {
    return occupiedPositionsMap.get(x).contains(y);
}

See? Wow! no need for null checks or other nonsense.

Note: This is relatively optimal from a performance point of view, but depending on your other needs, you may want to use Point objects as noted in other answers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜