Java if三元运算符和集合。emptylist ()

时间:2021-11-07 22:29:03

Could you please explain why with the first return type the code can't be compiled? The message is : Type mismatch: cannot convert from List<capture#1-of ? extends Object> to List<String>.

请解释为什么第一个返回类型的代码不能被编译?消息是:类型不匹配:不能从List <字符串> 列表。

Is there inserted an explicit cast in the second case ?

在第二种情况中是否插入了显式转换?

public class GenericsTest {

        private String getString() {
            return null;
        }

        public List<String> method() {
            String someVariable = getString();
            //first return type
            //return someVariable == null ? Collections.emptyList() : Collections.singletonList(someVariable);
            //second return type
            if (someVariable == null) {
                return Collections.emptyList();
            } else {
                return Collections.singletonList(someVariable);
            }
        }
    }

3 个解决方案

#1


17  

Because of type inference rules. I don't know why exactly (you should check the JSL, the ternary operator section), but it appears the ternary expression does not infer the type parameter from the return type.

因为类型推断规则。我不知道确切的原因(您应该检查JSL,三元运算符部分),但是三元表达式似乎没有从返回类型推断类型参数。

In other words, the type of the ternary expression depends on the types of its operands. But one of the operands has undetermined type parameter (Collections.emptyList()). At that point the ternary expression still does not have a type, so it cannot influence the type parameter. There are two types to be inferred - one is the result of the ternary expression, and the other is the type parameter of the .emptyList() method.

换句话说,三元表达式的类型取决于其操作数的类型。但是其中一个操作数有未确定的类型参数(Collections.emptyList())。此时三元表达式仍然没有类型,因此不能影响类型参数。有两种类型需要推断——一种是三元表达式的结果,另一种是. emptylist()方法的类型参数。

Use Collections.<String>emptyList() to explicitly set the type

使用collection . emptyList()显式设置类型

#2


2  

The type of the expression flag ? trueCase : falseCase is the most common type of the two cases.

表达式标志的类型?trueCase: falseCase是最常见的两种情况。

In this case the most common type of Collections.emptyList() and Collections.singletonList(someVariable) is List<? extends Object> because it cannot "see in the future" that Collections.emptyList() should return List<String> in the expression.

在这种情况下,最常见的集合类型是List . emptylist()和collection . singletonlist (someVariable)是List ,因为它不能“在未来看到”集合。emptylist()应该返回列表 在表达式中。


When you do:

当你做的事:

return Collections.emptyList();

the compiler can be smart and detect the type by the return type and check the correctness (inferred).

编译器可以通过返回类型检测类型并检查正确性(推断)。

#3


0  

Because Collections.emptyList() doesn't return List<String>. You set explicit the result of the method to List<String> and that means that you have to return a such list.

因为集合。emptylist()不返回List 。您将方法的结果显式地设置为List ,这意味着您必须返回这样的列表。

For example

例如

return Collection<String>.emptyList(); 

or

return new ArrayList<String>();

would work fine.

也可以。

#1


17  

Because of type inference rules. I don't know why exactly (you should check the JSL, the ternary operator section), but it appears the ternary expression does not infer the type parameter from the return type.

因为类型推断规则。我不知道确切的原因(您应该检查JSL,三元运算符部分),但是三元表达式似乎没有从返回类型推断类型参数。

In other words, the type of the ternary expression depends on the types of its operands. But one of the operands has undetermined type parameter (Collections.emptyList()). At that point the ternary expression still does not have a type, so it cannot influence the type parameter. There are two types to be inferred - one is the result of the ternary expression, and the other is the type parameter of the .emptyList() method.

换句话说,三元表达式的类型取决于其操作数的类型。但是其中一个操作数有未确定的类型参数(Collections.emptyList())。此时三元表达式仍然没有类型,因此不能影响类型参数。有两种类型需要推断——一种是三元表达式的结果,另一种是. emptylist()方法的类型参数。

Use Collections.<String>emptyList() to explicitly set the type

使用collection . emptyList()显式设置类型

#2


2  

The type of the expression flag ? trueCase : falseCase is the most common type of the two cases.

表达式标志的类型?trueCase: falseCase是最常见的两种情况。

In this case the most common type of Collections.emptyList() and Collections.singletonList(someVariable) is List<? extends Object> because it cannot "see in the future" that Collections.emptyList() should return List<String> in the expression.

在这种情况下,最常见的集合类型是List . emptylist()和collection . singletonlist (someVariable)是List ,因为它不能“在未来看到”集合。emptylist()应该返回列表 在表达式中。


When you do:

当你做的事:

return Collections.emptyList();

the compiler can be smart and detect the type by the return type and check the correctness (inferred).

编译器可以通过返回类型检测类型并检查正确性(推断)。

#3


0  

Because Collections.emptyList() doesn't return List<String>. You set explicit the result of the method to List<String> and that means that you have to return a such list.

因为集合。emptylist()不返回List 。您将方法的结果显式地设置为List ,这意味着您必须返回这样的列表。

For example

例如

return Collection<String>.emptyList(); 

or

return new ArrayList<String>();

would work fine.

也可以。