unbounded-wildcard

List<List<?>> and List<List> are incompatible types in java [duplicate]

删除回忆录丶 提交于 2019-12-01 00:08:16
问题 This question already has answers here : Cannot convert from List<List> to List<List<?>> (3 answers) Closed 4 years ago . I did not get this code to compile either way: List<List> a = new ArrayList(); List<List<?>> b = new ArrayList(); a = b; // incompatible types b = a; // incompatible types It seems that java does not consider List and List<?> to be the same type when it comes to generics. Why is that? And is there some nice way out? Context There is a library function with following

What is the difference between Collection<?> and Collection<T>

北慕城南 提交于 2019-11-28 22:55:44
I am mainly a C# developer and I was teaching Data Structures to my friend and they use Java in their University and I saw such an expression in Java: void printCollection(Collection<?> c) { for (Object e : c) { System.out.println(e); } } I haven't seen such a thing in C# so I wonder what's the difference between Collection<T> and Collection<?> in Java? void printCollection(Collection<T> c) { for (Object e : c) { System.out.println(e); } } I think it could have been written in the way above too. The guy in the documentation was comparing Collection<Object> and Collection<T> though. Examples

What is the difference between Collection<?> and Collection<T>

允我心安 提交于 2019-11-27 14:41:37
问题 I am mainly a C# developer and I was teaching Data Structures to my friend and they use Java in their University and I saw such an expression in Java: void printCollection(Collection<?> c) { for (Object e : c) { System.out.println(e); } } I haven't seen such a thing in C# so I wonder what's the difference between Collection<T> and Collection<?> in Java? void printCollection(Collection<T> c) { for (Object e : c) { System.out.println(e); } } I think it could have been written in the way above

Cannot convert from List<List> to List<List<?>>

怎甘沉沦 提交于 2019-11-26 22:44:35
A raw list converts to List<?> just fine. Why can't a list of raw lists convert to a list of List<?> ? { // works List raw = null; List<?> wild = raw; } { // Type mismatch: cannot convert from List<List> to List<List<?>> List<List> raw = null; List<List<?>> wild = raw; } Backstory (to mitigate the xy problem ): An API I'm using returns List<JAXBElement> . I happen to know that it is always List<JAXBElement<String>> . I plan to loop and build my own List<String> , but I was trying to fix (but not suppress) the raw type compiler warning when I write List<JAXBElement> raw = api(); . I tried: List

Java nested generic type

南笙酒味 提交于 2019-11-26 21:51:43
How come one must use the generic type Map<?, ? extends List<?>> instead of a simpler Map<?, List<?>> for the following test() method? public static void main(String[] args) { Map<Integer, List<String>> mappy = new HashMap<Integer, List<String>>(); test(mappy); } public static void test(Map<?, ? extends List<?>> m) {} // Doesn't compile // public static void test(Map<?, List<?>> m) {} Noting that the following works, and that the three methods have the same erased type anyways. public static <E> void test(Map<?, List<E>> m) {} Radiodef Fundamentally, List<List<?>> and List<? extends List<?>>

Difference between an unbound wildcard and a raw type

与世无争的帅哥 提交于 2019-11-26 20:22:02
I was reading about generics and I did not understand the need for unbound wildcards and how it differs from raw type. I read this question but still did not get it clearly. In the Java tutorial page for unbound wildcard I got below two points and I did not understood first point: If you are writing a method that can be implemented using functionality provided in the Object class. When the code is using methods in the generic class that don't depend on the type parameter. For example, List.size() or List.clear() . In fact, Class<?> is so often used because most of the methods in Class<T> do

Java nested generic type

大憨熊 提交于 2019-11-26 08:06:26
问题 How come one must use the generic type Map<?, ? extends List<?>> instead of a simpler Map<?, List<?>> for the following test() method? public static void main(String[] args) { Map<Integer, List<String>> mappy = new HashMap<Integer, List<String>>(); test(mappy); } public static void test(Map<?, ? extends List<?>> m) {} // Doesn\'t compile // public static void test(Map<?, List<?>> m) {} Noting that the following works, and that the three methods have the same erased type anyways. public static

Difference between an unbound wildcard and a raw type

*爱你&永不变心* 提交于 2019-11-26 06:37:20
问题 I was reading about generics and I did not understand the need for unbound wildcards and how it differs from raw type. I read this question but still did not get it clearly. In the Java tutorial page for unbound wildcard I got below two points and I did not understood first point: If you are writing a method that can be implemented using functionality provided in the Object class. When the code is using methods in the generic class that don\'t depend on the type parameter. For example, List