我必须在下面的 map 中放置一个整数值.

Map<String,?> map
map.put("key",2000);

当我运行上述代码时,我得到以下错误:

incompatible types: java.lang.Integer cannot be converted to capture#1 of ?

推荐答案

Unbounded个通用集合(Collection<?>个)是not writable个.让我们来探究原因.

未知类型

方括号<?>中的问号称为unknown type.对于编译器来说,这意味着actual type不能被预测,在运行时它可能看起来非常匹配任何东西:ObjectStringCat.因此,从unbounded collection中检索到的所有内容都将被视为Object:

Collection<?> items = new ArrayList<>();
Object item = items.iterator().next(); // this assignment is safe and will compile

但是don't confuseunbounded collectioncollection of objects.他们是incompatible.

Collection<Object>invariant behavior个,也就是说,它只希望再分配collectionObject类型.同时,我们可以将any类型的集合分配给Collection<?>.

Collection<?> items = new ArrayList<String>(); // OK
Collection<Object> objects = items; // that will not compile
Collection<Object> objects = new ArrayList<String>(); // that will not compile

所以unknown type可以用extendsObject表示.你可能会认为Collection<?>就像是upper-bounded系列Collection<? extends Object>.他们绝对是compatible岁.

Collection<?> items = new ArrayList<>();
Collection<? extends Object> upperBounded = items; // no issues
items = upperBounded; // no issues

修改无界集合

<?> <? extends Object>这样的通配符是not actual types.它们只是告诉编译器我们不知道type在运行时是什么.

那么百分之一百零一是什么呢?

因为已经很清楚unknown type(<?>)意味着any type可以延伸到Object,因此在Collection<?>的引擎盖下可能会出现ArrayList<Object>ArrayList<String>HashSet<BigDecimal>等等.

因为在ArrayList<String>中加Object,或者在HashSet<BigDecimal>编译器中加String都是illegal,所以任何试图在Collection<?>中加任何东西的try 都是prevent.没有与unknown type兼容的类型.

只有一个例外,null是任何类型对象的有效值,因此可以将其添加到unboundedupper-bounded集合中.

Collection<?> items = new ArrayList<>();
items.add(null);
System.out.println(items);
items.remove(null);
System.out.println(items.isEmpty());

将给出output分:

[null] - null-element was successfuly added
true   - i.e. isEmpty

Note:对于unboundedupper-bounded个集合,removal个元素都有no个限制.

Select

因此,有no way个新条目要添加到unbounded map,你可以一个接一个地复制它的条目,执行instanceOf判断到常规的泛型映射Map<String, Integer>,通过将映射分配给变量row type来消除泛型,然后在需要检索value时手动添加type-casts,如Java 1.4.

这就是如何将unbounded map的内容复制到regular,并利用泛型的优势.

Map<String, ?> unboundedMap = new HashMap<>()

Map<String, Integer> writableMap = new HashMap<>();

for (Map.Entry<String, ?> entry: unboundedMap.entrySet()) {
    if (entry.getValue() instanceof Integer) {
        writableMap.put(entry.getKey(), (Integer) entry.getValue());
    }
}

使用流API也是一样的

Map<String, Integer> writableMap =
       unboundedMap.entrySet().stream()
                   .filter(entry -> entry.getValue() instanceof Integer)
                   .collect(Collectors.toMap(Map.Entry::getKey,
                                             entry -> (Integer) entry.getValue()));

Java相关问答推荐

在Java 8之后,HashMap的最坏情况下时间复杂度仍然是O(n)而不是O(log n)?

JPackaged应用程序启动MSI调试,然后启动System. exit()

scanner 如何在执行hasNextLine一次后重新读取整个文件?

有没有一种方法使保持活动设置专用于java.net.http.HttpClient的一个实例

为什么一个Test的instance?& gt;在构造函数中接受非空对象?

有没有办法让扩展变得多态?

自定义批注的外推属性值

为什么我的回收视图会显示重复的列表?

扩展视图高度,并将其拖动到较低的视图上,而不是将其向下推?

JNI:将代码打包成自包含的二进制文件

在macOS上读取文件会导致FileNotFound,即使文件存在(并且具有权限)

垃圾回收器是否真的删除超出作用域的对象?

除0错误/抱歉我的句子是PT

本机方法(JNI)总是编译的吗?

Java HashMap保留所有时间复杂性

在Java Spring JPA中插入包含对其他实体的引用的列

如何使用jOOQ在PostgreSQL中从枚举类型生成Java枚举

没有Google Play服务,Firebase Auth无法工作

Java方法参数:括号中的类型声明?

在JPanel上使用GridBagLayout并将JButton放在里面时出现问题