Why does ternary operator fail with a type mismatch error?

妖精的绣舞 提交于 2021-02-07 09:26:54

问题


I have the following simple code piece:

List<XXXXBean> queryPeriodData()
{
    if (CollectionUtils.isEmpty(res))
    {
        return Collections.emptyList();
    }

    return res;
}

It works.

but if I change to this, there is a compile error...

return CollectionUtils.isEmpty(res) ? Collections.emptyList() : res;

error message is "Type mismatch: cannot convert from List< capture#1-of ? extends Object> to List< XXXXBean>"

I don't know the difference between the two way.


回答1:


try Collections.<XXXXBean>emptyList() in the statement



来源:https://stackoverflow.com/questions/25799249/why-does-ternary-operator-fail-with-a-type-mismatch-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!