【发布时间】:2013-07-03 17:11:35
【问题描述】:
我有一个方法接受一个参数Collection<Foo> foos,它可以是NULL。我想以ImmutableSet 作为输入的本地副本。现在我的代码如下所示:
if (foos == null)
{
this.foos = ImmutableSet.of();
}
else
{
this.foos = ImmutableSet.copyOf(foos);
}
有没有更清洁的方法来做到这一点?如果foos 是一个简单的参数,我可以执行Objects.firstNonNull(foos, Optional.of()) 之类的操作,但我不确定是否有类似处理集合的操作。
【问题讨论】: