Home > DeveloperSection > Forums > How can I auto box this class?
Andrew Deniel
Andrew Deniel

Total Post:29

Posted on    October-05-2013 3:59 AM

 Java Java 

 1 Reply(s)
 643  View(s)
Rate this:

I've the following class:

public class IntegerKey extends Number implements Comparable<IntegerKey> {
    private Integer m_key;
    public IntegerKey(Integer key) {
        m_key = key;
    public IntegerKey(int key) {
        m_key = key;

I would like to use this class as Follow:

assume I have the following generics:

Map<IntegerKey, MyCache> map = new HashMap<IntegerKey, MyCache>();
map.put(5, new MyCache());

This doesn't compile, why?? I don't want to do:

map.put(new IntegerKey(5), new MyCache());

Thank you!

Garry Winogrand
Garry Winogrand

Total Post:24

Posted on    October-05-2013 4:27 AM

Autoboxing only works for the primitives types and their respective counter parts in java.lang. In your example you could try dropping IntegerKey altogether and simply use Integer.        

Don't want to miss updates? Please click the below button!

Follow MindStick