ImmutableList.of正在工作,但突然停止工作

Dav*_*vid 1 java immutability

我有以下代码.不确定发生了什么,但它工作到1小时前我没有改变任何东西,现在它没有工作.

private final List<People> people;
private List<People> friend;

method() {
    friend= ImmutableList.of(people);
}
Run Code Online (Sandbox Code Playgroud)

我尝试编译时收到此错误消息:

incompatible types: inference variable E has incompatible bounds

[ERROR]     equality constraints: com.app.People
[ERROR]     lower bounds: java.util.List<com.app.People>
Run Code Online (Sandbox Code Playgroud)

任何的想法?谢谢

Pet*_*ček 5

你调用ImmutableList.of(E element)它返回一个ImmutableListE.在这种情况下,你的EList<People>(变量的类型people),所以最终的类型ImmutableList.of(people)List<List<People>>.

看起来你想创建一个副本people,为此还有另一种方法:ImmutableList.copyOf(Collection<E> elements)

因此,您的代码应如下所示:

friend = ImmutableList.copyOf(people);
Run Code Online (Sandbox Code Playgroud)