我有一个名为toSet()
的静态实用程序方法,它接收对象并返回Set
。无法推断类的类型<?>
public static <T> Set<T> toSet(T item, T... otherItems) {
Set<T> items = new HashSet<T>();
items.add(item);
items.addAll(Arrays.asList(otherItems));
return items;
}
的方法正常工作与其他对象,但会产生编译时,我想使Class<?>
的Set
。
Set<String> strings = toSet("OK", "This works well", "No problem");
Set<Integer> numbers = toSet(1, 2, 3, 4, 5);
但
Class<?> test = String.class;
Set<Class<?>> entities = toSet(test);
将产生Type mismatch: cannot convert from Set<Class<capture#5-of ?>> to Set<Class<?>>
。
难道我做错了什么吗?如何在不投射的情况下实现这一点?
谢谢先进。
找到更详细的信息:[在返回类型推断通配符泛型(可能重复的HTTP ://stackoverflow.com/questions/1294227/inferred-wildcard-generics-in-return-type) – 2012-07-18 03:27:45
@PaulBellora:非常感谢链接。这非常有帮助。 – Genzer 2012-07-18 06:09:28