0Pricing
Java Academy · Lesson

EnumSet

Fast set operations on enums.

EnumSet is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is EnumSet?

EnumSet is a specialized Set implementation for enum types only.

Internally it is a bit vector: each constant maps to one bit. This makes it extremely fast and memory-efficient compared to a HashSet of enums.

import java.util.EnumSet;
import java.util.Set;

public class Main {
    enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
    public static void main(String[] args) {
        Set<Day> weekend = EnumSet.of(Day.SAT, Day.SUN);
        System.out.println(weekend);
    }
}

Creating With of

The factory EnumSet.of(...) creates a set from listed constants. There are overloads for one through five elements plus a varargs version.

import java.util.EnumSet;

public class Main {
    enum Perm { READ, WRITE, EXECUTE }
    public static void main(String[] args) {
        EnumSet<Perm> perms = EnumSet.of(Perm.READ, Perm.WRITE);
        System.out.println(perms);
    }
}

allOf and noneOf

Two more factories:

  • allOf(Type.class) creates a set containing every constant.
  • noneOf(Type.class) creates an empty set ready to be filled.
import java.util.EnumSet;

public class Main {
    enum Color { RED, GREEN, BLUE }
    public static void main(String[] args) {
        EnumSet<Color> all = EnumSet.allOf(Color.class);
        EnumSet<Color> none = EnumSet.noneOf(Color.class);
        none.add(Color.RED);
        System.out.println("all: " + all);
        System.out.println("built: " + none);
    }
}

range for Contiguous Constants

EnumSet.range(from, to) includes all constants between two endpoints, inclusive, in declaration order.

import java.util.EnumSet;

public class Main {
    enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
    public static void main(String[] args) {
        EnumSet<Day> workdays = EnumSet.range(Day.MON, Day.FRI);
        System.out.println(workdays);
    }
}

complementOf

EnumSet.complementOf(set) returns a set of all constants not in the given set.

This is a fast way to invert a selection.

import java.util.EnumSet;

public class Main {
    enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
    public static void main(String[] args) {
        EnumSet<Day> work = EnumSet.range(Day.MON, Day.FRI);
        EnumSet<Day> rest = EnumSet.complementOf(work);
        System.out.println(rest);
    }
}

Set Operations

Because EnumSet is a real Set, you get union, intersection, and difference via addAll, retainAll, and removeAll.

These run as fast bitwise operations under the hood.

import java.util.EnumSet;

public class Main {
    enum Tag { A, B, C, D }
    public static void main(String[] args) {
        EnumSet<Tag> x = EnumSet.of(Tag.A, Tag.B, Tag.C);
        EnumSet<Tag> y = EnumSet.of(Tag.B, Tag.C, Tag.D);
        EnumSet<Tag> inter = EnumSet.copyOf(x);
        inter.retainAll(y);
        System.out.println("intersection: " + inter);
    }
}

Membership Tests

contains is a single bit check, so it is constant time and very fast.

import java.util.EnumSet;

public class Main {
    enum Feature { DARK_MODE, BETA, ANALYTICS }
    public static void main(String[] args) {
        EnumSet<Feature> enabled = EnumSet.of(Feature.DARK_MODE, Feature.BETA);
        System.out.println(enabled.contains(Feature.BETA));
        System.out.println(enabled.contains(Feature.ANALYTICS));
    }
}

EnumSet as a Flag Replacement

Before EnumSet, programmers used int bit flags like READ | WRITE. EnumSet replaces that with type safety and readability.

You get the performance of bit fields without the error-prone manual masking.

import java.util.EnumSet;
import java.util.Set;

public class Main {
    enum Style { BOLD, ITALIC, UNDERLINE }
    static void render(Set<Style> styles) {
        System.out.println("Applying: " + styles);
    }
    public static void main(String[] args) {
        render(EnumSet.of(Style.BOLD, Style.UNDERLINE));
    }
}

Iteration in Declaration Order

EnumSet iterates in the natural order of the constants, which is their declaration order.

This gives predictable, stable iteration unlike HashSet.

import java.util.EnumSet;

public class Main {
    enum Phase { ALPHA, BETA, RC, GA }
    public static void main(String[] args) {
        EnumSet<Phase> set = EnumSet.of(Phase.GA, Phase.ALPHA, Phase.RC);
        for (Phase p : set) System.out.println(p);
    }
}

Defensive Copies With copyOf

EnumSet.copyOf(collection) builds an EnumSet from any collection of enum values. Use it to make a safe copy or to convert a List.

import java.util.EnumSet;
import java.util.List;

public class Main {
    enum Role { ADMIN, EDITOR, VIEWER }
    public static void main(String[] args) {
        List<Role> source = List.of(Role.EDITOR, Role.VIEWER);
        EnumSet<Role> roles = EnumSet.copyOf(source);
        System.out.println(roles);
    }
}

Why It Is Fast

For enums with 64 or fewer constants, EnumSet uses a single long as the bit vector. Each operation is one or two machine instructions.

Always prefer EnumSet over HashSet<MyEnum> for enum sets.

import java.util.EnumSet;

public class Main {
    enum Bit { B0, B1, B2, B3 }
    public static void main(String[] args) {
        EnumSet<Bit> s = EnumSet.of(Bit.B0, Bit.B2);
        // contains is a single bit-and under the hood
        System.out.println(s.contains(Bit.B2));
    }
}

Quick Check

Test your EnumSet knowledge.

Recap

You learned EnumSet:

  • A bit-vector Set for enums, very fast and compact.
  • Factories: of, allOf, noneOf, range, complementOf, copyOf.
  • Iterates in declaration order.
  • The type-safe replacement for int bit flags.

Next, the matching map type: EnumMap.

import java.util.EnumSet;

public class Main {
    enum X { A, B }
    public static void main(String[] args) {
        System.out.println("EnumSet recap: " + EnumSet.allOf(X.class));
    }
}

Frequently asked questions

Is the “EnumSet” lesson free?

Yes — the full text of “EnumSet” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “EnumSet”?

Fast set operations on enums. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “EnumSet” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Enums with Fields and Methods
  2. Abstract Methods in Enums
  3. EnumSet
  4. EnumMap
← Back to Java Academy