Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map with integer as key returns null when long value is passed to the get method

import java.util.HashMap;
import java.util.Map;

public class StackOverFlowQuestion {

private static final int ERROR_CODE100 = -100;
private static final int ERROR_CODE101 = -101;
private static final int ERROR_CODE102 = -102;
private static final int ERROR_CODE103 = -103;
private static final int ERROR_CODE104 = -104;

public enum ErrorDetails {

    ERROR_CODE_100(ERROR_CODE100, "Error code 100 Desc", false),

    ERROR_CODE_101(ERROR_CODE101, "Error code 101 Desc", false),

    ERROR_CODE_102(ERROR_CODE102, "Error code 102 Desc", true),

    ERROR_CODE_103(ERROR_CODE103, "Error code 103 Desc", false),

    ERROR_CODE_104(ERROR_CODE104, "Error code 104 Desc", true);

    private int errorCode;
    private String errorMsg;
    private boolean canRetry;

    private ErrorDetails(int errorCode, String errorMsg, boolean canRetry) {
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
        this.canRetry = canRetry;
    }

    public String getErrorMsg() {
        return this.errorMsg;
    }

    public boolean canRetry() {
        return this.canRetry;
    }

    public String toString() {
        return "Error code : " + errorCode + ", errorMsg : " + errorMsg
                + ", canRetry : " + canRetry;
    }
}

private Map<Integer, ErrorDetails> errorMap;

public StackOverFlowQuestion() {
    System.out.println("StackOverFlowQuestion.StackOverFlowQuestion()");

    errorMap = new HashMap<Integer, StackOverFlowQuestion.ErrorDetails>();

    errorMap.put(ERROR_CODE100, ErrorDetails.ERROR_CODE_100);
    errorMap.put(ERROR_CODE101, ErrorDetails.ERROR_CODE_101);
    errorMap.put(ERROR_CODE102, ErrorDetails.ERROR_CODE_102);
    errorMap.put(ERROR_CODE103, ErrorDetails.ERROR_CODE_103);
    errorMap.put(ERROR_CODE104, ErrorDetails.ERROR_CODE_104);

    System.out.println("errorMap : " + errorMap);
}

/**
 * @param args
 */
public static void main(String[] args) {
    long param = -100;
    StackOverFlowQuestion question = new StackOverFlowQuestion();
    System.out.println("question.errorMap : " + question.errorMap);

    System.out.println("question.errorMap.containskey(param) : "
            + question.errorMap.containsKey(param));
    ErrorDetails errorDetails = question.errorMap.get(param);
    System.out.println("errorDetails : " + errorDetails);

    System.out.println("question.errorMap.containskey((int)param) : "
            + question.errorMap.containsKey((int) param));
    ErrorDetails errorDetailsWithInt = question.errorMap.get((int) param);
    System.out.println("errorDetailsWithInt : " + errorDetailsWithInt);

            int paramInt = -100;
    System.out.println("param == paramInt : " + (param == paramInt));
}

}

================================================================================= Output:

StackOverFlowQuestion.StackOverFlowQuestion()

errorMap : {-100=Error code : -100, errorMsg : Error code 100 Desc, canRetry : false, -102=Error code : -102, errorMsg : Error code 102 Desc, canRetry : true, -101=Error code : -101, errorMsg : Error code 101 Desc, canRetry : false, -104=Error code : -104, errorMsg : Error code 104 Desc, canRetry : true, -103=Error code : -103, errorMsg : Error code 103 Desc, canRetry : false}

question.errorMap : {-100=Error code : -100, errorMsg : Error code 100 Desc, canRetry : false, -102=Error code : -102, errorMsg : Error code 102 Desc, canRetry : true, -101=Error code : -101, errorMsg : Error code 101 Desc, canRetry : false, -104=Error code : -104, errorMsg : Error code 104 Desc, canRetry : true, -103=Error code : -103, errorMsg : Error code 103 Desc, canRetry : false}

question.errorMap.containskey(param) : false
errorDetails : null

question.errorMap.containskey((int)param) : true
errorDetailsWithInt : Error code : -100, errorMsg : Error code 100 Desc, canRetry : false

param == paramInt : true

=================================================================================

Here are few question which I need clarification

  1. The code is compiling even if I pass a long as the parameter to the get method of the HashMap which is declared to have only Integer as the keys. I was expecting a compilation error here, because I somehow feel this violates the strict typing.
  2. When I pass the long variable containing the error code as the parameter to the get method of the HashMap() the map returns null.
  3. When I downcast the same long parameter to an int and pass it to the get method of the hash map, the map returns the proper Enum.

I suspect the below line in the HashMap.get() method if (e.hash == hash && ((k = e.key) == key || key.equals(k)))

I am not sure whether int == long will fail or their corresponding wrappers will fail., i even added a check in the main method to verify the equality of the int and long variables.

I would like to understand the behavior here.

like image 871
user2042124 Avatar asked Feb 05 '13 08:02

user2042124


People also ask

Does map get return null?

Map get() method The most important Map function is map. get(key) which looks up the value for a key and returns it. If the key is not present in the map, get() returns null.

Can a key in a map be null?

Yes, null is always a valid map key for any type of map key (including primitives, sobjects, and user-defined objects).

How do you handle a null key on a map?

In Hashmap one null key is allowed and multiple null values are allowed. Since hashcode of null is 0 so null key actually stored at index 0 in hashmap. Hashmap in java actsually created as Array of linkedlist where index in array is find out by hashing on key.

What this get key method returns in map?

The get() method of Map interface in Java is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key.


1 Answers

The code is compiling even if I pass a long as the parameter to the get method of the HashMap which is declared to have only Integer as the keys. I was expecting a compilation error here, because I somehow feel this violates the strict typing.

Did you look at the signature of Map.get?

V get(Object key)

Any object can be used as the key. There are other Stack Overflow questions about that design decision; I'll find one later.

When I pass the long variable containing the error code as the parameter to the get method of the HashMap() the map returns null.

Yes, it would - because it will be boxed to a Long, and a Long isn't equal to an Integer. So the entry isn't found in the map.

When I downcast the same long parameter to an int and pass it to the get method of the hash map, the map returns the proper Enum.

Yes, it would - because then it will be boxed to an Integer, which will be equal to the appropriate key.

Basically you're being fooled by the fact that you can compare int and long values - that's just the compiler automatically promoting int to long for you; if you think of Integer and Long as entirely separate types, with no automatic conversion between them, the behaviour of your map makes perfect sense.

like image 78
Jon Skeet Avatar answered Oct 31 '22 00:10

Jon Skeet