Autoboxing versus manual boxing in Java
Why is the second piece of code faster? Map<Integer, Double> map = new HashMap<Integer, Double>(); for (int i = 0; i < 50000; i++) { for (double j = 0.0; j < 10000; j++) { map.put(i, j); } } Map<Integer, Double> map=new HashMap<Integer, Double>(); for (int i = 0; i < 50000; i++) { for (double j = 0.0; j < 10000; j++) { map.put(new Integer(i), new Double(j)); } } Autoboxing uses Integer.valueOf , which internally caches Integer objects for small integers (by default -128 to 127, but the max value can be configured with the "java.lang.Integer.IntegerCache.high" property - see the source code of