1

Beginner question: I have a hashmap which stores an array of integers as values. The key for each value is an object which consists of two integers (coordinate).

My question: how can I retrieve a value from the hashmap, based on the two coördinates within my object (my 'key')?

My Coords Class (with a little help from Eclipse):

    public class Coords {
    int x;
    int y;

    public Coords(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + x;
        result = prime * result + y;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Coords other = (Coords) obj;
        if (x != other.x)
            return false;
        if (y != other.y)
            return false;
        return true;
    }

}

Building the Hashmap:

public class BuildMap {

public Coords coords;
public int[] someData = new int[4];
public Random random = new Random();
HashMap<Coords, int[]> map = new HashMap<Coords, int[]>();

public void buildHashMap() {

    // coordinates from (0,0) to (31,31)
    for (int i = 0; i < 32; i++) {
        for (int j = 0; j < 32; j++) {

            coords = new Coords(i, j);

            // Every Coord gets a few random numbers
            for (int k = 0; k < 4; k++) {
                someData[k] = random.nextInt(8564);
            }

            map.put(coords, someData);

        }
    }

If I want to access the array on coordinates 12,13, how can I retrieve it? Is iteration needed (I hope not, I want to add 100,000+ coordinates and quick access ofcourse).

I was hoping this would work somewhat in the line of

int[] theValues = map.get(new Coords(12,13));

I hope you can help me. thanks in advance!

jlordo
  • 37,490
  • 6
  • 58
  • 83
user2150129
  • 55
  • 1
  • 2
  • 7
  • You posted the answer in the question: `int[] theValues = map.get(new Coords(12,13));` is how it works. – jlordo Jul 10 '13 at 19:49
  • 2
    Why do you think your get will not work? – Ayman Jul 10 '13 at 19:50
  • You've correctly overridden the `equals` method in your `Coords` class, so your `map.get` call ought to work as you want it to – Zim-Zam O'Pootertoot Jul 10 '13 at 19:51
  • @jlordo Thx for your answer, but I am getting the same values for every coordinate this way...? – user2150129 Jul 10 '13 at 19:52
  • You are getting the same answer because you're filling it with the same "random" number (if you specify the same seed, you get the same result) – CPerkins Jul 10 '13 at 19:54
  • @user2150129: You had an unrelated error. See my answer. – jlordo Jul 10 '13 at 19:56
  • Your `hashCode` is till problematic, see http://stackoverflow.com/questions/1894377/understanding-the-workings-of-equals-and-hashcode-in-a-hashmap – Jonathan Drapeau Jul 10 '13 at 20:05
  • @JonathanDrapeau: No, the `hashCode()` method is fine. As for the example you posted in your answer below, the hashcode of `(1, 13)` is 1005 and the hashcode of `(2, 13)` is 1036 using above function - they are not equal. – jlordo Jul 13 '13 at 07:07

2 Answers2

3

The problem is in how you construct the Map.

You are adding the same array as the value for each element.

You need to instantiate a new array for each element.

for (int i = 0; i < 32; i++) {
    for (int j = 0; j < 32; j++) {

        coords = new Coords(i, j);
        int[] someData = new int[4];   // <==== create a new array for each Map value
        // Every Coord gets a few random numbers
        for (int k = 0; k < 4; k++) {
            someData[k] = random.nextInt(8564);
        }

        map.put(coords, someData);

    }
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
1

You have one mistake: you are only using one array, and many references to it.

Move this line

public int[] someData = new int[4]; // without public 

above or below this line:

coords = new Coords(i, j);

to fix it.

jlordo
  • 37,490
  • 6
  • 58
  • 83