我有一個名爲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