简体   繁体   English

为什么三元运算符会因类型不匹配错误而失败?

[英]Why does ternary operator fail with a type mismatch error?

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>" 错误消息是“类型不匹配:无法从List <Capture#1-of?Extended Object>转换为List <XXXXBean>”

I don't know the difference between the two way. 我不知道两种方式之间的区别。

在语句中尝试Collections.<XXXXBean>emptyList()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM