繁体   English   中英

如何创建一个 ImmutableList 作为成员变量?

[英]How can I create an ImmutableList as a member variable?

我需要创建一个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是番石榴的一部分,所以你可以这样做:

private ImmutableList<String> stringList = ImmutableList.of("a", "b", "c");

您可以使用ImmutableList中的copyOf function

ImmutableList<String> stringList2 = ImmutableList.copyOf(stringList);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM