Skip to main content

How to Use the Unbounded Wildcard List<?> in Java Generics

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A Java method whose parameter is a List<?> accepts a list of any element type: a List<String>, a List<Integer> or anything else. The ? is the unbounded wildcard, and it's needed because a List<Object> parameter refuses a List<String>.

Syntax​

Syntax
static ReturnType methodName(List<?> name) {
// read elements as Object, or use size(), isEmpty() and the like
}
PartWhat it does
List<?>A list of unknown type, so the method accepts a list of any element type.
nameThe parameter's name. Its elements are read as Object.
ReturnType, methodNameThe method's return type and name, as in any method.

The Java Tutorials, Unbounded Wildcards: "The unbounded wildcard type is specified using the wildcard character (?), for example, List<?>. This is called a list of unknown type." It names two cases for it: "If you are writing a method that can be implemented using functionality provided in the Object class", and "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."

The Example​

The example's describe method takes a List<?> and reports its size and its first element. main passes it a List<String> and a List<Integer>. A changed version then declares the parameter as List<Object> instead.

Prerequisites​

  • A JDK 11 or later, which can run a single .java file directly. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java with a describe method that takes a list of unknown type and returns its size and first element, and a main method that calls it with a list of the roles editor and viewer and a list of the limits 10, 500 and 2000.

Main.java
import java.util.List;

public class Main {

// List<?> accepts a list of any element type.
static String describe(List<?> items) {
return items.size() + " items, the first is " + items.get(0);
}

public static void main(String[] args) {
List<String> roles = List.of("editor", "viewer");
List<Integer> limits = List.of(10, 500, 2000);

System.out.println(describe(roles));
System.out.println(describe(limits));
}
}

Step 2: Run It​

Terminal
java Main.java
Output
2 items, the first is editor
3 items, the first is 10

java Main.java compiles the file in memory and runs it. The command is the same in zsh, bash and PowerShell.

Step 3: Use List<Object> Instead​

Replace Main.java with a version whose describe takes a List<Object>:

Main.java
import java.util.List;

public class Main {

static String describe(List<Object> items) {
return items.size() + " items, the first is " + items.get(0);
}

public static void main(String[] args) {
List<String> roles = List.of("editor", "viewer");

System.out.println(describe(roles));
}
}
Terminal
java Main.java
Output
Main.java:12: error: method describe in class Main cannot be applied to given types;
System.out.println(describe(roles));
^
required: List<Object>
found: List<String>
reason: argument mismatch; List<String> cannot be converted to List<Object>
1 error
error: compilation failed

A zsh terminal where java Main.java prints 2 items, the first is editor and 3 items, the first is 10, then, for the changed file, fails to compile because a list of String cannot be converted to a list of Object.

What Happened​

  • describe worked for both lists. size() doesn't depend on the element type, and + turned the first element into text, whatever its type.
  • With List<Object>, the program didn't compile: List<String> cannot be converted to List<Object>. A list of strings isn't a list of objects to Java, although every string is an object. The tutorial says the same about its own example, which can't take List<Integer>, List<String> or List<Double> "because they are not subtypes of List<Object>".

The reason is what a method may put in. The tutorial: "You can insert an Object, or any subtype of Object, into a List<Object>. But you can only insert null into a List<?>." A List<?> parameter promises that the method won't add anything else, so any list is safe to pass.

Common Mistakes​

MistakeWhat happensDo this instead
List<Object> to mean "a list of anything"List<String> cannot be converted to List<Object>Use List<?>
Adding to a List<?>, as in items.add("x")error: no suitable method found for add(String), with String cannot be converted to CAP#1Take a List<String> when the method has to add strings
Reading an element of a List<?> as a String, as in String s = items.get(0);error: incompatible types: CAP#1 cannot be converted to StringRead it as an Object, and check its type with instanceof

Troubleshooting​

SymptomLikely causeFix
error: method describe in class Main cannot be applied to given types;, with reason: argument mismatch; List<String> cannot be converted to List<Object>The parameter is a List<Object>Make it a List<?>
error: no suitable method found for add(String) and String cannot be converted to CAP#1The code adds to a List<?>Give the parameter the element type the method adds
error: incompatible types: CAP#1 cannot be converted to StringAn element of a List<?> is read as a StringRead it as an Object

FAQ​

What is CAP#1 in the error?​

The compiler's name for the unknown type behind one ?. The error explains it: where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ?. All the compiler knows about CAP#1 is that it's some kind of Object.

Can I add anything to a List<?>?​

Only null, as the tutorial says. Adding null to a list of strings through a List<?> parameter compiled, and the list printed [a, null].

Conclusion​

List<?> lets a method accept a list of any element type, as long as it reads the elements as Object and uses only methods that don't depend on the type, such as size(). List<Object> isn't the same thing: it accepts only a List<Object>.

Additional Resources​