He complains because the compiler is trying to apply the cast to the first part of the ternary operator, and not to the whole expression. So this part of your code:
(List<String>) (allText != null)
This is what runs, but (allText != null) evaluates to boolean. To do a cast, you need to cover the whole expression, for example:
List<String> allHotels = (List<String>) ((allText != null) ? Arrays.asList(allText) : Collections.emptyList());
Note the brackets around the entire triple operator.
You don't really need to throw, but since the compiler will infer the correct type when you execute Collections.emptyList()
source share