【发布时间】:2021-01-08 20:40:24
【问题描述】:
我需要创建一个ImmutableList 作为成员变量。
这是我尝试过的:
private List<String> stringList = Arrays.asList("a", "b", "c");
private ImmutableList<String> stringList2 = Collections.unmodifiableList(stringList);
编译失败,报错:
FakeRemoteDataStore.java:59: error: incompatible types: no instance(s) of type variable(s) T exist so that List<T> conforms to ImmutableList<String>
private ImmutableList<String> stringList2 = Collections.unmodifiableList(stringList);
^
where T is a type-variable:
T extends Object declared in method <T>unmodifiableList(List<? extends T>)
如何创建ImmutableList 作为成员变量?
【问题讨论】:
-
ImmutableList是一个单独的类型,与java.util.Collections返回的内容不兼容。试试stringList2 = ImmutableList.copyOf(stringList)。 -
什么是
ImmutableList?一定是 Java SE 之外的东西。
标签: java arrays collections