Please find below code.
Map<Long,String> unsortMap = new HashMap<Long,String>();
unsortMap.put(new Long(123), "a");
unsortMap.put(new Long(123), "ab");
In above case map stores only 123 and ab. my understanding is key-value pairs are stored in buckets. each bucket will have unique number and is decided by key’s hashcode. in above code as both keys hashcodes are different, how does hashmap behaves internally?
Thanks!
In case of
LongthehashCodemethod is overriden to return the same value if the numeric values are the same. In fact, looking inside the source code, the method is this: