I am attempting to recreate a board game in Java which involves me storing a set of valid places pieces can be placed (for the AI). I thought that perhaps instead of storing as a list of Points, it would be run-time faster if I had an array/list/dictionary of the X coordinates in which there was an array/list of the y coordinates, so once you found the x coordinate you would only have to check its Ys not all the remaining points’.
The trouble I have is that i must change the valid points often. I came up with some possible solutions but have difficulty picking/implementing them:
- HashMap < Integer, ArrayList > with X as an integer key and the Ys as an ArrayList.
- Problem: I would have to create a new ArrayList every time I add an X.
- Also I am unsure about runtime performance of HashMap.
- int[X][Y] array initialized to the board size with each point set to its relative location (point 2,3 sets[2][3]) unset point being an invalid integer.
- Problem: I would have to iterate through all the points and check every point.
- List of Points This would simply be a Linked/Array List of Points.
- Problem: Lists are slower than arrays.
- How would using a Linked list of Points compare to checking the whole array like above?
Perhaps I should use a 2d linked list? What would be the fastest runtime way to do this?
You’re worrying about the wrong things. Accessing collection/map/array items is extremely fast. The graphical part will be way more performance-sensitive. Just use whatever data structure is most natural. It’s unlikely that you’re going to be storing enough items to really matter anyway. Build it first, then figure out where your performance problems really are.