Martin Frank Martin Frank - 8 months ago 24
Java Question

Designing a lookup map

I have a problem on designing a proper mapping.

i'm developing a top-down rpg game where you only see parts (chunks) of the map at runtime - when moving from one chunk to to another the new chunk data is loaded.

depending on my position different data is loaded.

currently looks my code like this (but certainly longer)

public static final int getChunkIdForPos(int x, int y){
switch (x) {
case 1248:
switch (y) {
case 1247: return R.raw.overworld_512;
case 1248: return R.raw.overworld_528;
case 1249: return R.raw.overworld_544;
default: break;}break;
case 1249:
switch (y) {
case 1247: return R.raw.overworld_513;
case 1248: return R.raw.overworld_529;
case 1249: return R.raw.overworld_545;
default: break;}break;
return R.raw.overworld_161;

problem is that this code is not easy to read and not easy to maintain. Any Ideas on how to do better?

i was thinking about using
to map data but that requires to create the Map first. That also means you carry lots of 'dead' objects as keys around (same for

Note: you can not determinate the returned values by any Math as suggested in this answer - Implemention a Lookup Table

the reason for choosing this design is to get an answer without creating any objects - the lookup just runs through two switch/case statements and provides an answer on the fly!


If you use a Point class with equals and hashcode implemented you can use them as keys in maps.

Now the actual lookup mechanism collapses to:

static final Map<Point, Integer> lookup = new HashMap<>();

static {
    // Looks a lot like your case statement eh?
    lookup.put(new Point(1248, 1247), 512);
    lookup.put(new Point(1248, 1248), 528);
    lookup.put(new Point(1248, 1249), 544);
    lookup.put(new Point(1249, 1247), 513);
    lookup.put(new Point(1249, 1248), 529);
    lookup.put(new Point(1249, 1249), 545);
    // Could also be populated by other statics.

public static final int getChunkIdForPos(int x, int y) {
    Integer got = lookup.get(new Point(x, y));
    return got != null ? got : 161;

The elegance of this is that the map is only ever built once. It is built at a predictable time (first reference to the class) and it can be added to by other modules (through a little tweaking).

Here's an example of a Point class that correctly implements equals and hashcode.

public static class Point {

    final int x;
    final int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;

    public String toString() {
        return "{" + x + "," + y + '}';

    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + this.x;
        hash = 97 * hash + this.y;
        return hash;

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        if (obj == null) {
            return false;
        if (getClass() != obj.getClass()) {
            return false;
        final Point other = (Point) obj;
        if (this.x != other.x) {
            return false;
        if (this.y != other.y) {
            return false;
        return true;