25

Assuming that you want to implement set theory concepts such as element, set, collection and relation in Java: How would you represent the empty set ?

Do I mislead myself, if I think of the NULL concept as it is used by the three-valued logic of databases?

eRaisedToX
  • 3,221
  • 2
  • 22
  • 28
M4TT4CK
  • 341
  • 1
  • 3
  • 4
  • 4
    I'm not sure there's a unique answer to this question; it depends on what you want to achieve. The obvious answer is "use an empty `Set`", but `NULL` might also be appropriate depending on what you're doing. – Oliver Charlesworth Jul 10 '12 at 13:36
  • 4
    There are myriad ways to represent the empty set, depending on the intended application. `Set = new HashSet()` is one of the simplest. – Fred Foo Jul 10 '12 at 13:37
  • 4
    Collections.emptySet() comes to mind ... – Sean Patrick Floyd Jul 10 '12 at 13:40
  • 1
    @OliCharlesworth: I'd say the absence of a set (you have zero sets), and an empty set (you have a set with zero elements) are not the same – Lukas Eder Jul 10 '12 at 13:42
  • A Set with no contents would work for me. I would not represent it in Java will a null. There could be some logic to this, but the code would be annoying since every time you referred to the collection you'd have to check for null. – Tony Ennis Jul 10 '12 at 13:43
  • @LukasEder I have never seen a case in my programming career where that distinction made a difference or was even helpful. – Tony Ennis Jul 10 '12 at 13:45
  • @TonyEnnis: Really? No set and an empty set are pretty different... – Oliver Charlesworth Jul 10 '12 at 13:46
  • @TonyEnnis: One example is lazy initialisation. The absence of the set means that it hasn't been initialised yet – Lukas Eder Jul 10 '12 at 13:49
  • @oli Perhaps mathematically. While coding? Never. Now, if empty *must* be implemented as something distinct, I'd choose a method that didn't involve me scattering null checks all over my code. – Tony Ennis Jul 10 '12 at 13:50
  • @TonyEnnis: Think about [`java.util.File.list()`](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#list()). It returns `null` if a file cannot provide its contained files (no set), or an empty array, if a file doesn't contain any files (empty set)... – Lukas Eder Jul 10 '12 at 14:08
  • @lukas I get it, but the practical difference is very slight. In your example, I can't show files no matter how it fails. In any event, I don't have a problem with a method returning a null versus an instance. However, the empty set IS a set (?right?) and assigning the null to some 'mySet' variable (which I believe is the use-case) would not be helpful at all. I'd sooner see an instance that behaves like a null-set than just using a Java null. In that regard, an empty Set would be more appropriate if mathematically imprecise. – Tony Ennis Jul 10 '12 at 16:11
  • Thank you all for your efforts. Now, I definitely know how to approach this problem. – M4TT4CK Jul 15 '12 at 20:25

2 Answers2

51

Use Collections.emptySet():

Returns the empty set (immutable). This set is serializable. Unlike the like-named field, this method is parameterized. This example illustrates the type-safe way to obtain an empty set:

 Set<String> s = Collections.emptySet();   

Implementation note: Implementations of this method need not create a separate Set object for each call. Using this method is likely to have comparable cost to using the like-named field. (Unlike this method, the field does not provide type safety.)

Wojciech Owczarczyk
  • 5,595
  • 2
  • 33
  • 55
15

Using null to represent an empty set is a bad idea. A null doesn't behave like a Set because (obviously) all attempts to perform an operation on it will throw a NullPointerException. That means that if you use null to denote an empty set, you code will be littered with tests for null ... and if you miss one, you've got a bug.

The practical solution is to use Collections.emptySet() if you want an immutable empty set, or create an instance of the appropriate Set class (e.g. new HashSet<>()) if you want a mutable set that starts out empty.


On rereading the question, I realize that you may have meant something different to my original understanding.

If you were trying to implement / model mathematical set theory concepts in Java from scratch, you would probably implement Set as an immutable class. (In mathematics, you don't mutate things!) The empty set is then just a Set instance with no elements in it. No special handling required.

The NULL concept is not required ... unless you are specifically trying to incorporate a "null", "undefined sets" or some similar concept in your mathematical model of sets. (If you are, I'm not sure we can advise you without understanding your model ... from a mathematical perspective.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216