什么是地图<?,?>是什么意思? [英] What does Map<?, ?> mean in Java?
问题描述
地图<?,?>
是什么意思在Java?
我在网上看过,但似乎找不到任何文章在上面。
What does Map<?, ?>
mean in Java?
I've looked online but can't seem to find any articles on it.
编辑:我在 MP3 Duration Java a>
推荐答案
?
表示一个占位符,感兴趣的(通配符):
?
indicates a placeholder in whose value you are not interested in (a wildcard):
HashMap<?, ?> foo = new HashMap<Integer, String>();
由于?
是通配符可以跳过它,仍然得到相同的结果:
And since ?
is a wildcard, you can skip it and still get the same result:
HashMap foo = new HashMap<Integer, String>();
但它们可以用于指定或子集要使用的泛型。在这个例子中,第一个泛型必须实现Serializable接口。
But they can be used to specify or subset the generics to be used. In this example, the first generic must implement the Serializable interface.
// would fail because HttpServletRequest does not implement Serializable
HashMap<? extends Serializable, ?> foo = new HashMap<HttpServletRequest, String>();
但最好使用具体类而不是这些通配符。如果你知道你在做什么,你应该只使用?
)。
But it's always better to use concrete classes instead of these wildcards. You should only use ?
if you know what your are doing :)
这篇关于什么是地图<?,?>是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!