我想使用 Guava 的不可变类型(例如ImmutableList
. 我还必须能够处理null
输入并将其视为空集合。
我能想到的最干净的是:
public void setStrings(List<String> strings) {
this.strings = strings == null ? ImmutableList.of() : ImmutableList.copyOf(strings);
}
有没有更易读的东西,最好没有三元运算符?Optional.of(strings).map(...).orElse(...)
由于我与此答案共享的推理,我不会认为这是一个不错的选择。