1

I have a strong python background and a total newbie with Java, so I try to do things in Pythonic way in java and end up struggling. Currently I have a problem to figure out how to do store objects, indexed by coordinates, so that they are iterable. I could create an 2-dimensional array of the objects, but I'm afraid that would take a lot of memory AND I would have to iterate over all the indexes every time (when I only want to do it occasionally) to get all the objects.

In python, I would use a dictionary with tuples as keys, but looks like such thing is not possible in Java.

E.g.

class bar()
    pass

bar1 = bar()
bar2 = bar()
# etc

foo = { (0,1): bar1, (100,30): bar2 }

for x in foo.values():
    do_something()
Cœur
  • 32,421
  • 21
  • 173
  • 232
Kimvais
  • 34,273
  • 14
  • 100
  • 135

1 Answers1

1
  1. Do not hesitate for memory (not now, maybe later)

  2. The Java equivalent "class" is a Map. As Map is an interface take a HashMap class for the moment.

  3. Map has a method values() to get all values of the map.

  4. As your key is not scalar you may create your own Coordinates class. Be aware to override equals() and hashCode().

Community
  • 1
  • 1
PeterMmm
  • 22,286
  • 12
  • 68
  • 104