4

I have a program which needs to store a couple of simple int values and associate each with a String key. Currently I initialize this member like so:

private Map<String, int[]> imageSizes = new HashMap<String, int[]>();

and then append to it with something like imageSizes.put("some key", new int[]{100, 200});

My question is this - is there a way to give these values a fixed length? I will only ever need 2 elements in each. Java doesn't like the syntax if I try to give the arrays a length in the member definition.

Furthermore, is there any benefit to restricting the array length in this case, or am I just being overzealous in my optimisation?

4
  • I doubt if there is a benefit. The Map is simply storing a reference to the value, not a copy of the value. I don't think there is any optimization happening at all. Commented Sep 23, 2011 at 5:44
  • I always thought creating a new int[100] would allocate memory for 100 values, whereas new int[] would allocate some amount declared to be 'sufficient' by the compiler and resized when it runs out. Not so in Java? All of this only applies to primitive types, of course... Commented Sep 28, 2011 at 7:45
  • 1
    new int[] is only legal in a construction where the array elements follow in braces and the compiler can count how many. My point is that in the Map, the amount of memory used for the values is just the size of the reference, no matter how large the array is itself. So fixing the size of the array won't change the amount of memory used by the map. In C, you could declare a pointer and then allocate more or less memory to it (or forget to allocate any and crash); Java doesn't work like that. Commented Sep 28, 2011 at 14:22
  • awesome. if you make that a proper answer i'll mark it as accepted & useful (: Commented Sep 29, 2011 at 2:09

4 Answers 4

3

new int[] is only legal in a construction where the array elements follow in braces and the compiler can count how many. In the Map, the amount of memory used for the values is just the size of the reference (32 or 64 bits), no matter how large the array is itself. So fixing the size of the array won't change the amount of memory used by the map. In C, you could declare a pointer and then allocate more or less memory to it (or forget to allocate any and crash); Java manages memory for you.

Sign up to request clarification or add additional context in comments.

Comments

3

You could wrap it in a simple, reuseable and self-documenting class:

public class Size {

    private int width;
    private int height;

    public Size(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    // Add setters if necessary.
}

And use it as follows:

Map<String, Size> sizes = new HashMap<String, Size>();
sizes.put("some key", new Size(100, 200));
// ...

3 Comments

Since the class is intended to be used in a HashMap, isn't it necessary (or at least recommended) to override hashCode and equals?
@Gabriel: that only applies to keys. String has them already overriden.
That would certainly be the way of doing it neatly, thanks... I guess my original question wasn't that clear, it was more of a low-level memory optimisation question than an architecture one (:
3

You could use a class the extends HashMap and provides this special functionality:

public class ImageHashMap extends HashMap<String, int[]> {
    public void putArray(String key, int a, int b) {
      put(key, new int[]{a, b});
    }
}

To call:

ImageHashMap im = new ImageHashMap();
im.putArray("some key", 100, 200);

Comments

0

In addition to Bohemian's solution, you might want to protect the original put method from HashMap like this:

@Override
public int[] put(String key, int[] value) {
    throw new UnsupportedOperationException("Sorry, operation not allowed.");
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.