0

我有以下课程:

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;
    }

}

我想使用这个类作为关注:

假设我有以下泛型:

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

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

这不编译,为什么?我不想这样做:

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

谢谢你。

4

4 回答 4

5

这不编译,为什么?

因为没有从intto的隐式转换IntegerKey。您不能在 Java 中创建用户定义的隐式转换。你被语言定义的那些卡住了。

要么必须以某种方式显式获取 a IntegerKey,要么必须将地图的类型更改为Map<Integer, MyCache>.

于 2012-10-04T13:09:34.683 回答
2

这不编译,为什么?

只有原始类型会自动装箱到它们的包装器中。Java 中不允许其他组合。

我不想这样做:

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

在这种情况下,不要使用 KeyInteger,只需使用 Integer。

于 2012-10-04T13:09:51.250 回答
2

自动装箱仅适用于java.lang. 在您的示例中,您可以尝试IntegerKey完全删除并简单地使用Integer.

于 2012-10-04T13:10:04.010 回答
0

Seeing the comment concerning the special functionality of IntegerKey, it seems logical to wrap the functionality you are asking for in a new class (MHashMap in the following examples):

import java.util.HashMap;

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;
  }
  //...Functionality
  //...required methods to include to actually test
  @Override
  public int compareTo (IntegerKey arg0) {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public double doubleValue () {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public float floatValue () {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public int intValue () {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public long longValue () {
    // TODO Auto-generated method stub
    return 0;
  }

}
class MyCache {}
class MHashMap extends HashMap<IntegerKey, MyCache> {
  public void put (int key, MyCache value) {
    super.put(new IntegerKey(key), value);
  }
  public void put (Integer key, MyCache value) {
    super.put(new IntegerKey(key), value);
  }
  //...Useful to include other wrappings
  public MyCache get (int key) {
    return super.get(new IntegerKey(key));
  }
  public MyCache get (Integer key) {
    return super.get(new IntegerKey(key));
  }
}
public class AutoboxFix {
  public static void main (String[] args) {
    //needed to explicitly declare map as MHashMap for "override" to work
    MHashMap map = new MHashMap();
    //compiles
    map.put(5, new MyCache());
    //... get probably works with your extended functionality
  }
}

This should provide the functionality your looking for. The get method will depend on the methods in Comparable. There may be more methods that you want to reimplement to auto cast from int or Integer to IntegerKey, but you should be able to use the rest of the HashMap functionality normally. It is necessary to declare map as MHashMap type, because using Map won't compile. It would be sensible to separate classes into their own files and make them public.

This approach won't work if it's absolutely necessary to declare map as Map<IntegerKey, MyCache>. You could make a static method where ever your using map and such to do the casting for you:

public class AClass {
  public static void myPutMethod (Map<Integer, MyCache> map, int key,
       MyCache value) {
    map.put(new IntegerKey(key), value);
  }
}

Use:

myPutMethod(map, 5, new MyCache());

Then you can define more methods for Integer and Map.get.

于 2013-08-20T17:25:43.167 回答