What is the purpose of a List <?> If you can only insert a null value?
Based on the information provided in the link , it is said that:
It is important to note that
List<Object>andList<?>Do not match. You can insert an object or any subtype of an object into aList<Object>. But you can only embed null inList<?>.
What is the use of using List<?> When you can only insert null ?
For instance,
methodOne(ArrayList<?> l): We can use this method for an ArrayList any type, but inside a method that we cannot add to the list except null .
l.add(null);//(valid) l.add("A");//(invalid) You use unlimited wildcards when a list (or collection) has unknown types.
As the textbook says, it is used when you want to get information about a list, for example, print its contents, but you donβt know what type it may contain:
public static void printList(List<?> list) { for (Object elem: list) System.out.print(elem + " "); System.out.println(); } You should not use it if you want to insert values ββinto the list, because the valid value is null , because you do not know what values ββit contains.