1

I have a valueclass (position) with two doubles (x and y) in it. I also have a class ship that has an attribute with a position object in it.

I need to be able to do do a lookup like: get ship at (5,7) with constant time. I also want to be able to change the value of a position object because multiple ships can refer to the same position objects and they all have to move together when that position object changes.

I have looked at hashmap, but to be able to do get ship at new Position(5,7) I need to override the hashcode of the position object (so that positions with the same values have the same hash), and I heard you shouldn't change the hashcode of an object in a hashlist.

Frederik Baetens
  • 781
  • 1
  • 9
  • 20
  • 2
    Overriding the `hashCode ()` method doesn't mean you change the hashcode of the object in the map. It only means you control how the hashcode is being generated. – Laf Apr 06 '17 at 18:04
  • Take a look at [guava `Table`](https://github.com/google/guava/wiki/NewCollectionTypesExplained#table) –  Apr 06 '17 at 18:04
  • @Laf: if the `hashCode()` is based on the values and the values can change, the hashcode can change. The question states that the `Position` objects are supposed to be mutable, it is reasonable. – Holger Apr 06 '17 at 18:05
  • How is the supposed answer to “get ship at (5,7)” if multiple ships are supposed to be able to share the same position? – Holger Apr 06 '17 at 18:09
  • @Laf, yes the hash has to be based on the values of the position because that seems like the only way to be able to do a lookup of something at (5,7) with new Position(5,7), because the objects aren't the same, but the hashtable has to consider them the same because i'm interested in all objects with the values (5,7) And you can solve the multiple position issue with values that are lists of ships, but that's not relevant because hashmaps won't work anyways because of the changing hash – Frederik Baetens Apr 06 '17 at 18:10
  • @smurfendrek123 : Could it be an array(list) instead of a hashtable ? – Luatic Apr 06 '17 at 18:13
  • @user7185318 no because I really need constant lookup time – Frederik Baetens Apr 06 '17 at 18:13
  • 1
    Whatever lookup data structure you’ll use, there is no way around *updating* it whenever a ship’s position changes. A `HashMap` isn’t worse than others, you may use a mutable key type as long as you are disciplined enough to never change the objects currently used as keys. If you want more robust software, use two different position classes, the mutable ship position and an immutable key class representing a snapshot of the current position. – Holger Apr 06 '17 at 18:26
  • @Holger, something like this then? http://imgur.com/a/BmUAv – Frederik Baetens Apr 06 '17 at 18:41
  • 1
    That seems to go into the right direction. – Holger Apr 06 '17 at 19:24

1 Answers1

1

This might not be exactly what you are looking for but I think what you need is a 2D spatial partitioning data structure like a quad-tree.

rad
  • 128
  • 1
  • 7