Scrivio Interview library
CodingIntermediate6 min read · updated 12 November 2026

Replace this flag representation with EnumSet and show how to persist it compactly: int permissions = READ | WRITE; // constants 1, 2, 4, 8

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.

Java
static final int READ = 1, WRITE = 2, DELETE = 4, ADMIN = 8;
int permissions = READ | WRITE; // 3
boolean canWrite = (permissions & WRITE) != 0; // true
permissions |= DELETE; // 7
permissions = 42; // compiles: meaningless bits
grant(user, WRITE | 16); // compiles: 16 is not a permission
System.out.println(permissions); // 42: unreadable in logs

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.

Java
enum Permission { READ, WRITE, DELETE, ADMIN }
EnumSet<Permission> perms = EnumSet.of(Permission.READ, Permission.WRITE);
perms.contains(Permission.WRITE); // true
perms.add(Permission.DELETE); // [READ, WRITE, DELETE]
perms.remove(Permission.READ); // [WRITE, DELETE]
System.out.println(perms); // [WRITE, DELETE]: readable
void grant(User u, Set<Permission> p) { }
grant(user, EnumSet.of(Permission.WRITE)); // only permissions can be passed

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.

Java
EnumSet<Permission> admin = EnumSet.allOf(Permission.class); // [READ, WRITE, DELETE, ADMIN]
EnumSet<Permission> none = EnumSet.noneOf(Permission.class); // []
EnumSet<Permission> notWrite = EnumSet.complementOf(EnumSet.of(Permission.WRITE)); // [READ, DELETE, ADMIN]
EnumSet<Permission> upToDelete = EnumSet.range(Permission.READ, Permission.DELETE); // [READ, WRITE, DELETE]
EnumSet<Permission> merged = EnumSet.copyOf(perms);
merged.addAll(EnumSet.of(Permission.READ)); // union, like a | b
merged.retainAll(upToDelete); // intersection, like a & b
boolean hasAll = merged.containsAll(EnumSet.of(Permission.READ, Permission.WRITE)); // true
Int flags and their EnumSet equivalents
Int flagsEnumSet
READ | WRITEEnumSet.of(READ, WRITE)
(p & WRITE) != 0p.contains(WRITE)
p |= qp.addAll(q)
p &= qp.retainAll(q)
p & ~WRITEp.remove(WRITE)
~p & ALL_BITSEnumSet.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.

Java
// Fragile: the bit is the ordinal
int mask = 0;
for (Permission p : perms) mask |= 1 << p.ordinal(); // [WRITE, DELETE] -> 6
// after inserting SHARE before DELETE, 6 is read back as [WRITE, SHARE]

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.

Java
enum Permission {
READ(0), WRITE(1), DELETE(2), ADMIN(3); // bits are fixed forever, even if order changes
final int bit;
Permission(int bit) { this.bit = bit; }
}
@Converter
class PermissionsConverter implements AttributeConverter<Set<Permission>, Integer> {
public Integer convertToDatabaseColumn(Set<Permission> perms) {
int mask = 0;
for (Permission p : perms) mask |= 1 << p.bit;
return mask; // [READ, WRITE] -> 3
}
public Set<Permission> convertToEntityAttribute(Integer mask) {
EnumSet<Permission> out = EnumSet.noneOf(Permission.class);
for (Permission p : Permission.values()) if ((mask & (1 << p.bit)) != 0) out.add(p);
return out; // 3 -> [READ, WRITE]
}
}

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.

What to say in the interviewAbout 80 seconds aloud

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