我有一個方法,採用參數Collection<Foo> foos
,它可能是NULL。我想以輸入的本地副本作爲ImmutableSet
。現在我的代碼看起來像這樣:乾淨番石榴的方式來處理可能爲null的收集
if (foos == null)
{
this.foos = ImmutableSet.of();
}
else
{
this.foos = ImmutableSet.copyOf(foos);
}
有沒有更乾淨的方法來做到這一點?如果foos
是一個簡單的參數,我可以做一些像Objects.firstNonNull(foos, Optional.of())
但我不確定是否有類似的東西處理集合。
+1可能值得指出的是'copyOf'足夠聰明,只要它是一個'ImmutableSet'就可以返回輸入。 –
@PaulBellora我相信你剛剛做到了。 ':)' –
另外'ImmutableSet。 ()'可能是不必要的。 –