This is jdk1.7.0_04.
I tried to use Collections.emptyList()instead new, using my own empty list in a conditional expression:
List<String> list = (anArray != null) ? Arrays.asList(anArray) : Collections.emptyList();
but get the following error:
error: incompatible types
List<String> list = (anArray != null) ? Arrays.asList(anArray) : Collections.emptyList();
^
required: List<String>
found: List<CAP
where CAP
CAP
1 error
I was able to understand that I need to change things:
List<String> list = (anArray != null) ? Arrays.asList(anArray) : Collections.<String>emptyList();
But as part of the work on this, I came across a bizarre (to me, anyway) situation that:
List<String> alwaysEmpty = Collections.emptyList();
compiles fine, but:
List<String> alwaysEmpty = (List<String>) Collections.emptyList();
gives the following compilation error:
error: inconvertible types
List<String> alwaysEmpty = (List<String>) Collections.emptyList();
^
required: List<String>
found: List<Object>
What the hell?
Now I understand that, for some strange reason, using a conditional operator somehow blocks the type inference system from implementing what the type parameter emptyList()should be for the call String, and therefore it must be explicitly specified. But why is (admittedly, excessive) pretense added?