gpt4 book ai didi

java - Guava 库的 Lists.newArraylist() 是如何工作的?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:41:57 25 4
gpt4 key购买 nike

我想了解 Lists.newArrayList() 如何知道要返回的列表类型。我看到了 source code对于函数 newArrayList(),但它只是返回泛型 E 的 ArrayList

public static <E> ArrayList<E> newArrayList() {
return new ArrayList<E>();
}

但是,当我调用该函数时,我不会传递任何此类信息。

List<String> testList = Lists.newArrayList();

它怎么知道我想要什么类型的ArrayList

我阅读了有关泛型和 TypeToken 的内容,但无法通过代码与之相关。

最佳答案

因为编译器可以从变量声明中推断出类型。

例子:

List<String> list = Lists.newArrayList(),

编译器将理解集合的类型(E 的类型)将为 <String> ,因为您希望获得一个字符串列表。

避免重写整个 <> 参数很有用,但是对于 Java7 和菱形运算符,您可以使用

避免它
List<String> list = new ArrayList<>();

(想象这是一个 List<List<String>> 你应该重写 List<List<String>> )

我为你找到了这个:

The Java compiler takes advantage of target typing to infer the type parameters of a generic method invocation. The target type of an expression is the data type that the Java compiler expects depending on where the expression appears. Consider the method Collections.emptyList, which is declared as follows:

static <T> List<T> emptyList();

Consider the following assignment statement:

List<String> listOne = Collections.emptyList();

This statement is expecting an instance of List; this data type is the target type. Because the method emptyList returns a value of type List, the compiler infers that the type argument T must be the value String. This works in both Java SE 7 and 8. Alternatively, you could use a type witness and specify the value of T as follows:

List<String> listOne = Collections.<String>emptyList();

However, this is not necessary in this context. It was necessary in other contexts, though. Consider the following method:

void processStringList(List<String> stringList) {
// process stringList
}

Suppose you want to invoke the method processStringList with an empty list. In Java SE 7, the following statement does not compile:

processStringList(Collections.emptyList());

The Java SE 7 compiler generates an error message similar to the following:

List<Object> cannot be converted to List<String>

The compiler requires a value for the type argument T so it starts with the value Object. Consequently, the invocation of Collections.emptyList returns a value of type List, which is incompatible with the method processStringList. Thus, in Java SE 7, you must specify the value of the value of the type argument as follows:

processStringList(Collections.<String>emptyList());

This is no longer necessary in Java SE 8. The notion of what is a target type has been expanded to include method arguments, such as the argument to the method processStringList. In this case, processStringList requires an argument of type List. The method Collections.emptyList returns a value of List, so using the target type of List, the compiler infers that the type argument T has a value of String. Thus, in Java SE 8, the following statement compiles:

processStringList(Collections.emptyList());

See Target Typing in Lambda Expressions for more information.

阅读this

关于java - Guava 库的 Lists.newArraylist() 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23615451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com