3

I am trying to map and array of ints to an int value. I know that int[] wont work as keys. I have tried List however that doesn't work as well. Is there anyway I can do this? Thanks.

Here is my failed attempt:

    private void createMap(){
    List<Integer> state_action_pair = new ArrayList<Integer>();
    for(int i=0;i<this.stateActionTable.length;i++){
        for(int j=0;j<this.stateActionTable[0].length;j++){
            state_action_pair.add(this.stateActionTable[i][j]);
        }
        this.stateActionMap.put(state_action_pair, i);
        state_action_pair.clear();
    }
}
7
  • int[] can work as a key for a hashmap just fine. A List<Integer> on the otherhand doesn't. It isn't clear why you are trying to do, and why it fails. You also leave off the relevant information, what is stateActionMap? Commented Nov 11, 2015 at 9:54
  • @matt no. An int[] is only equal to itself. A List is equal to any other list containing equal elements in the same order. Commented Nov 11, 2015 at 10:02
  • @JBNizet exactly the point. A List, the .equals will change over time, making it unsuitable for a hashmap. An int[] will remain the same. Commented Nov 11, 2015 at 10:05
  • 2
    @matt It won't change over time if the list isn't mutated. The whole point is to be able to do map.get(Arrays.asList(1, 2, 3)). That is impossible to do with an array, because new int[] {1, 2, 3} won't ever be equal to the array used as key in the map. Commented Nov 11, 2015 at 10:06
  • It's not about array or list but about mutability. HashMap/HashSet docs both say that behaviour is undefined if the keys are modified. If the list is immutable, it can be safely used as a key. Commented Nov 11, 2015 at 10:07

1 Answer 1

3

Your problem is that you use a single ArrayList instance for all the keys of your Map. You need an individual instance for each key :

private void createMap(){
  for(int i=0;i<this.stateActionTable.length;i++){
    List<Integer> state_action_pair = new ArrayList<Integer>();
    for(int j=0;j<this.stateActionTable[0].length;j++){
        state_action_pair.add(this.stateActionTable[i][j]);
    }
    this.stateActionMap.put(state_action_pair, i);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Note that using mutable data structures as keys in a map is typically a big no-no.
@aioobe As long as the OP doesn't mutate them, everything will work fine. Of course, using an immutable List would be safer.
Do i have to save all these instances or is it fine that they get deleted after the function returns? thanks
@Kenadams They are not deleted as long as the Map is not deleted, since the Map keeps references to them.
@Eran then why not prevent the list from being mutated, and use Collections.unmodifiableList(state_action_pair) as key?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.