简体   繁体   English

在 Java 集合映射中<Key,?> “?”是什么意思参考?

[英]In Java Collections Map<Key,?> What does “?” refer to?

In Java Collections I saw something like this: Map<Key,?> .在 Java Collections 中,我看到了这样的东西: Map<Key,?> I don't know how it is working, can anyone help me out with this or provide an example?我不知道它是如何工作的,谁能帮我解决这个问题或提供一个例子?

The question mark (?) represents an unknown type.问号 (?) 表示未知类型。

In your example, Map<Key, ?> , it means that it will match a map containing values of any type.在您的示例中, Map<Key, ?> ,这意味着它将匹配包含任何类型值的映射。 It does not mean you can create a Map<Key, ?> and insert values of any type in it.并不意味着您可以创建Map<Key, ?>并在其中插入任何类型的值。

Quoting from the documentation :文档中引用:

In generic code, the question mark (?), called the wildcard, represents an unknown type.在泛型代码中,称为通配符的问号 (?) 表示未知类型。 The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable;通配符可用于多种情况:作为参数、字段或局部变量的类型; sometimes as a return type (though it is better programming practice to be more specific).有时作为返回类型(尽管更具体的是更好的编程实践)。 The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.通配符永远不会用作泛型方法调用、泛型类实例创建或超类型的类型参数。

For example, say you want to create a function that will print the values of any map, regardless of the value types:例如,假设您想创建一个函数来打印任何地图的值,而不管值类型如何:

static void printMapValues(Map<String, ?> myMap) {
    for (Object value : myMap.values()) {
        System.out.print(value + " ");
    }
}

Then call this function passing a Map<String, Integer> as argument:然后调用这个函数,传递一个Map<String, Integer>作为参数:

Map<String, Integer> myIntMap = new HashMap<>();
myIntMap.put("a", 1);
myIntMap.put("b", 2);
printMapValues(myIntMap);

And you would get:你会得到:

1 2

The wildcard allows you to call the same function passing a Map<String, String> , or any other value type, as argument:通配符允许您调用传递Map<String, String>或任何其他值类型作为参数的相同函数

Map<String, String> myStrMap = new HashMap<>();
myStrMap.put("a", "one");
myStrMap.put("b", "two");
printMapValues(myStrMap);

Result:结果:

one two

This wildcard is called unbounded , since it gives no information about the type.此通配符称为unbounded ,因为它不提供有关类型的信息。 There are a couple of scenarios where you may want to use the unbounded wildcard:有几种情况您可能想要使用无界通配符:

  • If you're calling no methods except those defined in the Object class.如果您不调用Object类中定义的方法以外的任何方法。
  • When you're using methods that don't depend on the the type parameter, such as Map.size() or List.clear() .当您使用不依赖于类型参数的方法时,例如Map.size()List.clear()

A wildcard can be unbounded, upper bounded, or lower bounded:通配符可以是无界、上界或下界:

  • List<?> is an example of an unbounded wildcard . List<?>无界通配符的一个例子。 It represents a list of elements of unknown type.它表示未知类型的元素列表。

  • List<? extends Number> List<? extends Number> is an example of an upper bounded wildcard . List<? extends Number>是一个上限通配符的例子。 It matches a List of type Number , as well as its subtypes, such as Integer or Double .它匹配Number类型的List及其子类型,例如IntegerDouble

  • List<? super Integer> List<? super Integer> is an example of a lower bounded wildcard . List<? super Integer>下界通配符的一个例子。 It matches a List of type Integer , as well as its supertypes, Number and Object .它匹配Integer类型的List及其超类型NumberObject

The Unknown Wildcard未知的通配符

? can be any dataType可以是任何数据类型

List<?> means a list typed to an unknown type , This could be a List<Integer> , a List<Boolean> , a List<String> etc. List<?>表示类型为未知类型的List<Integer> ,这可能是List<Integer>List<Boolean>List<String>等。

Now coming to your example Map<Key,?> means Value which is to be inserted in this map can be of any data Type.现在来到你的榜样Map<Key,?>手段Value是在这个地图中插入可以是任何数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM