How to implement the empty set - ∅? [closed]
Asked Answered
G

2

27

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?

Gnaw answered 10/7, 2012 at 13:34 Comment(12)
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.Dizzy
There are myriad ways to represent the empty set, depending on the intended application. Set<E> = new HashSet<E>() is one of the simplest.Nosing
Collections.emptySet() comes to mind ...Foretell
@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 sameSelah
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.Galligan
@LukasEder I have never seen a case in my programming career where that distinction made a difference or was even helpful.Galligan
@TonyEnnis: Really? No set and an empty set are pretty different...Dizzy
@TonyEnnis: One example is lazy initialisation. The absence of the set means that it hasn't been initialised yetSelah
@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.Galligan
@TonyEnnis: Think about java.util.File.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)...Selah
@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.Galligan
Thank you all for your efforts. Now, I definitely know how to approach this problem.Gnaw
D
54

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.)

Doubleminded answered 10/7, 2012 at 13:46 Comment(1)
Thank you for your suggestion. I will use your advice.Gnaw
R
16

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.)

Retinitis answered 10/7, 2012 at 14:53 Comment(1)
+1 for stating why the null is a bad idea.Galligan

© 2022 - 2024 — McMap. All rights reserved.