48
private static HashMap<Integer, Bitmap> mBitmapCache;         
mBitmapCache.put(R.drawable.bg1,object);                       

R.drawable.bg1 is an int ... but i want to convert into Integer because Hashmap takes an Integer... and when draw the multiple objects in seconds , it creates a Integer Object runtime which affects the performence of the code...

Sriram
  • 10,298
  • 21
  • 83
  • 136
user643144
  • 517
  • 1
  • 4
  • 5

5 Answers5

74
int iInt = 10;
Integer iInteger = Integer.valueOf(iInt);

P.S. Answer edited due to comments pointing out an issue with initial suggested solution.

Olegas
  • 10,349
  • 8
  • 51
  • 72
  • 12
    "You should not call the constructor for wrapper classes directly, such as`new Integer(42)`. Instead, call the valueOf factory method, such as Integer.valueOf(42). This will typically use less memory because common integers such as 0 and 1 will share a single instance." Integer iInteger = Integer.valueOf(iInt); – user3439968 Oct 03 '15 at 15:01
  • 1
    i think this is done automatically, thus you could have somethink like `Integer iInteger = iInt;` and it should work. – Ionut Negru Sep 22 '16 at 13:34
  • 1
    I just wanted to share https://stackoverflow.com/q/3131136/384674 for "You should not call constructor..." – Betlista Dec 15 '18 at 20:14
  • 3
    Just for curiosity, is `Integer value = (Integer) intValue;` a bad practise? Or, it's not recommended for int to Integer conversion? – Simon Z. Mar 20 '19 at 01:32
  • It is bad practice to convert it through the wrapper classes directly. – Simran Sharma Mar 01 '21 at 07:11
  • @SimranSharma you are wrong see this question:https://stackoverflow.com/questions/67088328/directly-casting-primitive-type-to-object-is-fine-in-java – Chaitanya Karmarkar Apr 14 '21 at 08:55
  • @ChaitanyaKarmarkar, I guess you haven't seen the edits to this answer. The older answer used the constructor of the wrapper class to convert it. Somewhat like this `new Integer(int);`. Using wrapper class in this way to cast is a bad practice. You should use `Integer.valueOf(int)` instead. It is a function built for this purpose and is more reliable and efficient. – Simran Sharma Apr 17 '21 at 03:56
  • @SimranSharma You are correct about new Integer(int); This one is bad practice. But Integer value = (Integer) intValue; is not a bad practice. It is Autoboxing that is what I want to say. – Chaitanya Karmarkar Apr 17 '21 at 12:41
  • 1
    @ChaitanyaKarmarkar, absolutely that is not a bad practice. I said it because, in the previous edit, he used `new Integer(int);`. But, I feel when there is a method that is provided dedicated for one purpose, it always makes more sense to use the method instead. I feel it always works out to be more efficient that way (in general, not being specific to Integer casting). – Simran Sharma Apr 17 '21 at 12:47
  • I agree with you. – Chaitanya Karmarkar Apr 19 '21 at 12:19
26

As mentioned, one way is to use

int original = 32;
Integer converted = new Integer(original);

But you should not call the constructor for wrapper classes directly. It is a poor practice to do so. Instead, use the methods pre-defined especially for this purpose.

So, the new code would look like this (recommended):

mBitmapCache.put(Integer.valueOf(R.drawable.bg1), object);
Simran Sharma
  • 852
  • 7
  • 16
Morphing Coffee
  • 815
  • 1
  • 10
  • 20
  • 2
    Is there a reason "you should not call the constructor for wrapper classes directly"? – Jacob Holloway Apr 25 '17 at 22:56
  • 1
    Not really - it is just a guess that the dev did not have an urge to create new object on every call. Rarely the case: You would use new Integer() if you want to distinguish several Integer objects. Mostly the case: You are using Integer and not int just because you want to also have *null* value. Integer.valueOf() would return you the same object on multiple calls in particular scenario, which will save memory (never the case with new Integer()). – Morphing Coffee Apr 26 '17 at 00:14
  • 2
    I just wanted to share https://stackoverflow.com/q/3131136/384674 for "You should not call constructor..." – Betlista Dec 15 '18 at 20:14
1

I had a similar problem . For this you can use a Hashmap which takes "string" and "object" as shown in code below:

/** stores the image database icons */
public static int[] imageIconDatabase = { R.drawable.ball,
        R.drawable.catmouse, R.drawable.cube, R.drawable.fresh,
        R.drawable.guitar, R.drawable.orange, R.drawable.teapot,
        R.drawable.india, R.drawable.thailand, R.drawable.netherlands,
        R.drawable.srilanka, R.drawable.pakistan,

};
private void initializeImageList() {
    // TODO Auto-generated method stub
    for (int i = 0; i < imageIconDatabase.length; i++) {
        map = new HashMap<String, Object>();

        map.put("Name", imageNameDatabase[i]);
        map.put("Icon", imageIconDatabase[i]);
    }

}

Vicky Kapadia
  • 6,025
  • 2
  • 24
  • 30
0

One of the way is by using following
Integer obj = new Integer(primitiveValue) or in your case new Integer(R.drawable.bg1). The only problem with this is it would unnecessarily create an objects during runtime.

If you are using JDK 5 or above you can exercise power of Autoboxing.
Lets keep that aside for a while.

Within class Integer (java.lang.Integer) we do have many methods one of which is:
static Integer valueOf(int) it returns value of an int as a Integer.

So a Integer.valueOf(R.drawable.bg1) could work.

obrutus
  • 63
  • 4
-4

i it integer, int to Integer

Integer intObj = new Integer(i);

add to collection

list.add(String.valueOf(intObj));
ppasler
  • 3,579
  • 5
  • 31
  • 51
IvanT
  • 1
  • 3
    When your trying to answer a question which has is already being answered. Add more information to the answer so that it adds value. – Vishnu667 Jan 21 '17 at 19:49