java map 里怎么才能放不同类型的key,比如 put(1, 100),put("1", "满分")
public class MyDictionary<K, V> implements DictionaryInterface<K, V> {
private Map<K, V> map;
public MyDictionary() {
map = new HashMap<>();
}
public void add(K key, V value) {
map.put(key, value);
}
public V get(K key) {
return map.get(key);
}
}
public class Main {
public static void main(String[] args) {
MyDictionary<Integer, String> dictionary = new MyDictionary<>();
dictionary.add(1, 100);
dictionary.add("1", "满分") // 报错了
}
}
1.将Key和value定义为Object即可