Quick revision
The short answer
We define enum Permission { READ, WRITE, DELETE, ADMIN } and replace int flags with EnumSet<Permission>, for example EnumSet.of(READ, WRITE), using contains(), addAll(), complementOf() and range() instead of bit masks. For storage, we avoid a bitmask built from ordinal(), because reordering the enum silently changes the stored meaning; we store a list of names, or a bitmask whose bit position is defined explicitly on each constant. EnumSet keeps the speed of a bit vector while adding type safety and readable code.
What to remember
In this answer, we replaced int flags with an enum and EnumSet, which keeps the bit-vector performance while giving type safety, readable output and named operations such as contains(), addAll(), retainAll(), complementOf() and range(). For persistence, we saw that a bitmask built from ordinal() silently changes meaning when constants are reordered, and stored either the names or a bitmask based on a bit position defined explicitly on each constant, mapped with an AttributeConverter and pinned by a test.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We define enum Permission { READ, WRITE, DELETE, ADMIN } and replace int flags with EnumSet<Permission>, for example EnumSet.of(READ, WRITE), using contains(), addAll(), complementOf() and range() instead of bit masks. For storage, we avoid a bitmask built from ordinal(), because reordering the enum silently changes the stored meaning; we store a list of names, or a bitmask whose bit position is defined explicitly on each constant. EnumSet keeps the speed of a bit vector while adding type safety and readable code.
Why this question is asked
Int flags combined with | are a common legacy pattern: compact and fast, but easy to misuse and hard to read. Interviewers ask us to replace them with EnumSet because it shows we know the modern tool, and then ask about persistence because that is where most refactorings go wrong. In this answer, we will look at the problems with int flags, rewrite them with EnumSet, go through the operations we use every day, and then design storage that stays correct when the enum changes.
1What Is Wrong With Int Flags
With int flags, each permission is a power of two, so each one occupies a single bit, and a set of permissions is the bitwise OR of its members.
The representation is efficient, but the type is just int. The compiler cannot stop a meaningless value, a constant from a different flag set, or a typo such as & instead of |. Logs show numbers that someone must decode by hand, and there is no easy way to iterate over the permissions in a set.
2The Same Logic With EnumSet
EnumSet is a Set implementation for enum constants that is itself a bit vector: for enums of up to 64 constants it keeps a single long, with one bit per ordinal. So we keep the performance and gain a real type.
The method now accepts a Set<Permission>, so nothing but permissions can be passed. toString() prints names in declaration order, which makes logs and test failures readable. Iteration with a for loop goes through the permissions in declaration order too.
3Everyday Set Operations
Every bit trick from the int version has a named method. Because both sets use the same bit-vector representation underneath, these operations are still single long operations for small enums.
| Int flags | EnumSet |
|---|---|
| READ | WRITE | EnumSet.of(READ, WRITE) |
| (p & WRITE) != 0 | p.contains(WRITE) |
| p |= q | p.addAll(q) |
| p &= q | p.retainAll(q) |
| p & ~WRITE | p.remove(WRITE) |
~p & ALL_BITS | EnumSet.complementOf(p) |
Integer.bitCount(p) | p.size() |
Two notes. range() depends on declaration order, so it only makes sense when the order is meaningful, such as levels. And EnumSet is mutable, so a set exposed from an object should be copied or wrapped with Collections.unmodifiableSet() to stop callers from changing it.
4Persisting It: Why an Ordinal-Based Bitmask Is Fragile
The tempting way to store an EnumSet compactly is to turn it back into an int using each constant's ordinal as the bit position. That works until someone inserts a constant, for example SHARE between WRITE and DELETE. DELETE's ordinal moves from 2 to 3, so every stored value with bit 2 set now means SHARE instead of DELETE. Users silently gain and lose permissions, which is a security incident, not just a bug.
So the stored form must not depend on declaration order. We have two good choices.
5Two Safe Ways to Store It
The first is to store names, for example as a comma-separated column or a join table. It is readable in the database and survives reordering. The cost is more space and a rule that constants are never renamed. The second keeps a compact bitmask but defines the bit position explicitly on each constant, so it is part of the data contract rather than an accident of order.
Notice that the converter never touches ordinal(). A new constant gets the next unused bit, such as SHARE(4), regardless of where it is declared. We pin the bits with a unit test, as with any stored code, and we reject masks with unknown bits on read if the data could come from a newer version. If the database must be queried by permission, a join table with one row per permission name is often better than a mask, because SQL can then index it.
6Follow-up questions
Is EnumSet really as fast as int flags?
Very close. For enums of up to 64 constants, contains, add and the bulk operations are single long operations, the same work the int version does. The only extra costs are the EnumSet object itself and a type check, which are negligible outside the tightest loops.
How do you send an EnumSet in JSON?
Jackson writes it as an array of names, such as ["READ","WRITE"], and reads it back into a Set. That is readable and order-independent, so it is usually the right choice for APIs, with the same tolerance rules for unknown names as any enum in a contract.
What if the enum grows beyond 32 or 64 permissions?
EnumSet switches to a long array automatically. A stored int mask runs out at 32 bits and a long at 64, which is another reason names or a join table are a better default for persistence.
7Conclusion
In this answer, we replaced int flags with an enum and EnumSet, which keeps the bit-vector performance while giving type safety, readable output and named operations such as contains(), addAll(), retainAll(), complementOf() and range(). For persistence, we saw that a bitmask built from ordinal() silently changes meaning when constants are reordered, and stored either the names or a bitmask based on a bit position defined explicitly on each constant, mapped with an AttributeConverter and pinned by a test.
Open with
I would define enum Permission with READ, WRITE, DELETE and ADMIN and replace the int with an EnumSet of Permission, so READ or WRITE becomes EnumSet.of READ and WRITE.
Then, if they want more
Checking a flag becomes contains, merging becomes addAll, intersection is retainAll, clearing a bit is remove, and there are complementOf, allOf, noneOf and range for the rest.
Under the hood EnumSet for up to 64 constants is a single long with one bit per ordinal, so these are still one or two machine operations, but now the compiler only accepts permissions, toString prints names, and methods can take a Set of Permission instead of a bare int.
For persistence I would not build a mask from ordinal, because if someone inserts a constant in the middle, every stored bit after it changes meaning, and for permissions that is a security problem.
I would either store names, in a column or a join table, which is readable and order-independent but means constants are never renamed, or keep a compact mask but give each constant an explicit, permanent bit number in its constructor and convert with a JPA AttributeConverter. Either way, I pin the stored values with a unit test.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Replace this flag representation with EnumSet and show how to persist it compactly: int...": https://scrivio-interview-library.netlify.app/java/enums/1bc86737b8/