Collections.emptyList()返回一个List< Object&gt ;? [英] Collections.emptyList() returns a List<Object>?

查看:106
本文介绍了Collections.emptyList()返回一个List< Object&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在推导泛型类型参数时,我在浏览Java规则时遇到了一些麻烦。考虑下面的类,它有一个可选的列表参数:

I'm having some trouble navigating Java's rule for inferring generic type parameters. Consider the following class, which has an optional list parameter:

import java.util.Collections;
import java.util.List;

public class Person {
  private String name;
  private List<String> nicknames;

  public Person(String name) {
    this(name,Collections.emptyList());
  }

  public Person(String name,List<String> nicknames) {
    this.name = name;
    this.nicknames = nicknames;
  }
}

我的Java编译器出现以下错误:

My Java compiler gives the following error:

Person.java:9: The constructor Person(String, List<Object>) is undefined

但是 Collections.emptyList()返回类型< T>列表< T> ,而不是 List< Object> 。添加转换不会帮助

But Collections.emptyList() returns type <T> List<T>, not List<Object>. Adding a cast doesn't help

public Person(String name) {
  this(name,(List<String>)Collections.emptyList());
}

产生

Person.java:9: inconvertible types

使用 EMPTY_LIST 而不是 emptyList()

public Person(String name) {
  this(name,Collections.EMPTY_LIST);
}

产生

Person.java:9: warning: [unchecked] unchecked conversion

以下更改会导致错误消失:

Whereas the following change makes the error go away:

public Person(String name) {
  this.name = name;
  this.nicknames = Collections.emptyList();
}

任何人都可以解释我在这里遇到的类型检查规则,并解决它的最佳方法?在这个例子中,最终的代码示例是令人满意的,但是对于更大的类,我希望能够按照这个可选参数模式编写方法,而不需要重复代码。

Can anyone explain what type-checking rule I'm running up against here, and the best way to work around it? In this example, the final code example is satisfactory, but with larger classes, I'd like to be able to write methods following this "optional parameter" pattern without duplicating code.

额外的信用:什么时候适合使用 EMPTY_LIST 而不是 emptyList()

For extra credit: when is it appropriate to use EMPTY_LIST as opposed to emptyList()?

推荐答案

您遇到的问题是即使方法 emptyList()返回 List< T> ,你没有提供类型,所以它默认返回 List< Object> 。您可以提供类型参数,并让您的代码像预期的那样运行,如下所示:

The issue you're encountering is that even though the method emptyList() returns List<T>, you haven't provided it with the type, so it defaults to returning List<Object>. You can supply the type parameter, and have your code behave as expected, like this:

public Person(String name) {
  this(name,Collections.<String>emptyList());
}

现在,当您进行直分配时,编译器可以计算出通用为你输入参数。这就是所谓的类型推断。例如,如果你这样做:

Now when you're doing straight assignment, the compiler can figure out the generic type parameters for you. It's called type inference. For example, if you did this:

public Person(String name) {
  List<String> emptyList = Collections.emptyList();
  this(name, emptyList);
}

然后 emptyList() call会正确地返回一个 List< String>

then the emptyList() call would correctly return a List<String>.

这篇关于Collections.emptyList()返回一个List&lt; Object&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆