让我们首先考虑一个简单的场景(请参阅ideone.com上的完整源代码):
import java.util.*;
public class TwoListsOfUnknowns {
static void doNothing(List<?> list1, List<?> list2) { }
public static void main(String[] args) {
List<String> list1 = null;
List<Integer> list2 = null;
doNothing(list1, list2); // compiles fine!
}
}
Run Code Online (Sandbox Code Playgroud)
这两个通配符是不相关的,这就是为什么你可以doNothing用a List<String>和a 调用List<Integer>.换句话说,两者?可以指代完全不同的类型.因此,以下不编译,这是预期的(也在ideone.com上):
import java.util.*;
public class TwoListsOfUnknowns2 {
static void doSomethingIllegal(List<?> list1, List<?> list2) {
list1.addAll(list2); // DOES NOT COMPILE!!!
// The method addAll(Collection<? extends capture#1-of ?>)
// in the type List<capture#1-of …Run Code Online (Sandbox Code Playgroud) 在这个例子中:
import java.util.*;
public class Example {
static void doesntCompile(Map<Integer, List<? extends Number>> map) {}
static <T extends Number> void compiles(Map<Integer, List<T>> map) {}
static void function(List<? extends Number> outer)
{
doesntCompile(new HashMap<Integer, List<Integer>>());
compiles(new HashMap<Integer, List<Integer>>());
}
}
Run Code Online (Sandbox Code Playgroud)
doesntCompile() 无法编译:
Example.java:9: error: incompatible types: HashMap<Integer,List<Integer>> cannot be converted to Map<Integer,List<? extends Number>>
doesntCompile(new HashMap<Integer, List<Integer>>());
^
Run Code Online (Sandbox Code Playgroud)
whilecompiles()被编译器接受。
这个答案解释了唯一的区别是不同<? ...>,<T ...>让您稍后引用类型,这似乎并非如此。
是什么区别<? extends Number>,并<T extends Number>在这种情况下,为什么不第一编译?
我主要是一名C#开发人员,我正在向我的朋友教授Data Structures,他们在他们的大学里使用Java,我在Java中看到了这样一个表达式:
void printCollection(Collection<?> c) {
for (Object e : c) {
System.out.println(e);
}
}
Run Code Online (Sandbox Code Playgroud)
我还没有在C#中看到这样的东西,所以我想知道Java Collection<T>和Collection<?>Java 之间的区别是什么?
void printCollection(Collection<T> c) {
for (Object e : c) {
System.out.println(e);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为它也可以用上面的方式编写.在文档中的家伙比较Collection<Object>和Collection<T>虽然.
示例来自http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html
我有一个 RandomizedWrapper 类。此类包含一个接受列表的构造函数:
public class RandomizedWrapper{
final int upperBound = 100;
final List<RandomizerEntry<?>> randomizeList;
Map<Integer, RandomizerEntry<?>> randomizerMap;
/**
* Construct a new RandomizedWrapper instance
*
* @param randomizeList - A list containing all randomizable objects
*/
public RandomizedWrapper(final List<RandomizerEntry<?>> randomizeList) {
this.randomizeList = randomizeList;
this.randomizerMap = new HashMap<>();
}
}
Run Code Online (Sandbox Code Playgroud)
我想创建这个 RandomizedWrapper 类的新实例。我通过以下代码:
List<RandomizerEntry<ItemStack>> randomizerList = new ArrayList<>();
//stuff here
RandomizedWrapper wrapper = new RandomizedWrapper(randomizerList);//error
Run Code Online (Sandbox Code Playgroud)
当我尝试创建这个新对象时,遇到以下错误:
The constructor RandomizedWrapper(List<RandomizerEntry<ItemStack>>) is undefined
Run Code Online (Sandbox Code Playgroud)
这没有任何意义。我非常清楚地在 RandomizedWrapper 类中有一个接受List<RandomizerEntry<?>>通配符的构造函数。编译器提出的解决方案是“ create a constructor that …
所以我通过官方的java教程,https://docs.oracle.com/javase/tutorial/java/generics/index.html,也通过stackoverflow搜索,结果发现使用<E>和之间没有太大的区别<?>,一个我可以理解的是普通的通用形式,另一个是通配符.到目前为止,我遇到的唯一区别是,当使用<E>或<E extetnds BlaBlaClass>我们可以引用类型时E,否则我们根本不知道有关集合或数组或类型的任何信息.
我的问题是:使用<?>(通配符)优于普通泛型是否有任何优势<E>?如果是这样,这种情况的情况是什么?为什么有人会使用通配符呢?
我看过泛型类型和通配符类型之间的区别,区别?(通配符)和Java中的Type参数,何时使用泛型方法以及何时使用通配符?,Java泛型?,E和T有什么区别?.到目前为止,它似乎<?>是较差的版本<E>
generics ×5
java ×5
wildcard ×2
arraylist ×1
collections ×1
constructor ×1
dictionary ×1
jvm ×1
types ×1