Collections.emptyList() returns a List<Object>?
Solution 1:
The issue you're encountering is that even though the method emptyList()
returns List<T>
, you haven't provided it with the type, so it defaults to returning List<Object>
. You can supply the type parameter, and have your code behave as expected, like this:
public Person(String name) {
this(name,Collections.<String>emptyList());
}
Now when you're doing straight assignment, the compiler can figure out the generic type parameters for you. It's called type inference. For example, if you did this:
public Person(String name) {
List<String> emptyList = Collections.emptyList();
this(name, emptyList);
}
then the emptyList()
call would correctly return a List<String>
.
Solution 2:
You want to use:
Collections.<String>emptyList();
If you look at the source for what emptyList does you see that it actually just does a
return (List<T>)EMPTY_LIST;
Solution 3:
the emptyList method has this signature:
public static final <T> List<T> emptyList()
That <T>
before the word List means that it infers the value of the generic parameter T from the type of variable the result is assigned to. So in this case:
List<String> stringList = Collections.emptyList();
The return value is then referenced explicitly by a variable of type List<String>
, so the compiler can figure it out. In this case:
setList(Collections.emptyList());
There's no explicit return variable for the compiler to use to figure out the generic type, so it defaults to Object
.
Solution 4:
Since Java 8 this kind of code compiles as expected and the type parameter gets inferred by the compiler.
public Person(String name) {
this(name, Collections.emptyList()); // Inferred to List<String> in Java 8
}
public Person(String name, List<String> nicknames) {
this.name = name;
this.nicknames = nicknames;
}
The new thing in Java 8 is that the target type of an expression will be used to infer type parameters of its sub-expressions. Before Java 8 only direct assignments and arguments to methods where used for type parameter inference.
In this case the parameter type of the constructor will be the target type for Collections.emptyList()
, and the return value type will get chosen to match the parameter type.
This mechanism was added in Java 8 mainly to be able to compile lambda expressions, but it improves type inferences generally.
Java is getting closer to proper Hindley–Milner type inference with every release!