I have a small function which does this
val s = MutableHashSet[DateTime]()
if (a != null) s.add(a)
if (b != null) s.add(b)
if (c != null) s.add(c)
if (d != null) s.add(d)
s
I know using null is bad (and I can use option) but let us forget that for now and concentrate more upon how to make this HashSet immutable. MutableHashSet
is the same as scala.collection.mutable.HashSet
. I saw a similar question here. However creating multiple Seq
and then a hashset from it looks like too much of an overkill.