Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java EnumSet: Creation, Operations, and Performance Tips

Java EnumSet

This guide walks you through creating, manipulating, and optimizing Java EnumSet collections with hands‑on code examples and best‑practice insights.

The EnumSet class is a high‑performance set implementation designed exclusively for enum types. It implements the java.util.Set interface and also inherits Cloneable and Serializable, making it both versatile and efficient.

Before diving into EnumSet, make sure you’re comfortable with Java enums, as the set is tightly coupled to a single enum type.

Java EnumSet: Creation, Operations, and Performance Tips


Creating an EnumSet

Unlike other set implementations, EnumSet does not expose public constructors. Instead, you use its static factory methods to build a set.

1. Using EnumSet.allOf()

The allOf() method creates a set that contains every constant of the specified enum type.

import java.util.EnumSet;

class Main {
    enum Size { SMALL, MEDIUM, LARGE, EXTRALARGE }

    public static void main(String[] args) {
        EnumSet<Size> sizes = EnumSet.allOf(Size.class);
        System.out.println("EnumSet: " + sizes);
    }
}

Output

EnumSet: [SMALL, MEDIUM, LARGE, EXTRALARGE]

2. Using EnumSet.noneOf()

Creates an empty set that can only contain values of the specified enum type.

import java.util.EnumSet;

class Main {
    enum Size { SMALL, MEDIUM, LARGE, EXTRALARGE }

    public static void main(String[] args) {
        EnumSet<Size> sizes = EnumSet.noneOf(Size.class);
        System.out.println("Empty EnumSet: " + sizes);
    }
}

Output

Empty EnumSet: []

Note: The resulting set accepts only Size constants because it was instantiated with Size.class.

3. Using EnumSet.range()

Builds a set containing all enum constants from a lower bound to an upper bound, inclusive.

import java.util.EnumSet;

class Main {
    enum Size { SMALL, MEDIUM, LARGE, EXTRALARGE }

    public static void main(String[] args) {
        EnumSet<Size> sizes = EnumSet.range(Size.MEDIUM, Size.EXTRALARGE);
        System.out.println("EnumSet: " + sizes);
    }
}

Output

EnumSet: [MEDIUM, LARGE, EXTRALARGE]

4. Using EnumSet.of()

Creates a set containing one or more specific enum constants.

import java.util.EnumSet;

class Main {
    enum Size { SMALL, MEDIUM, LARGE, EXTRALARGE }

    public static void main(String[] args) {
        EnumSet<Size> sizes1 = EnumSet.of(Size.MEDIUM);
        System.out.println("EnumSet1: " + sizes1);
        EnumSet<Size> sizes2 = EnumSet.of(Size.SMALL, Size.LARGE);
        System.out.println("EnumSet2: " + sizes2);
    }
}

Output

EnumSet1: [MEDIUM]
EnumSet2: [SMALL, LARGE]

Common Operations on EnumSet

Adding Elements

import java.util.EnumSet;

class Main {
    enum Size { SMALL, MEDIUM, LARGE, EXTRALARGE }

    public static void main(String[] args) {
        EnumSet<Size> all = EnumSet.allOf(Size.class);
        EnumSet<Size> selected = EnumSet.noneOf(Size.class);
        selected.add(Size.MEDIUM);
        System.out.println("After add(): " + selected);
        selected.addAll(all);
        System.out.println("After addAll(): " + selected);
    }
}

Output

After add(): [MEDIUM]
After addAll(): [SMALL, MEDIUM, LARGE, EXTRALARGE]

Note that addAll() accepts any Collection<Size>, including lists or sets, as long as they contain the same enum type.

Iterating Elements

Use an Iterator to traverse the set:

import java.util.EnumSet;
import java.util.Iterator;

class Main {
    enum Size { SMALL, MEDIUM, LARGE, EXTRALARGE }

    public static void main(String[] args) {
        EnumSet<Size> sizes = EnumSet.allOf(Size.class);
        Iterator<Size> it = sizes.iterator();
        System.out.print("EnumSet: ");
        while (it.hasNext()) {
            System.out.print(it.next() + ", ");
        }
    }
}

Output

EnumSet: SMALL, MEDIUM, LARGE, EXTRALARGE, 

Removing Elements

import java.util.EnumSet;

class Main {
    enum Size { SMALL, MEDIUM, LARGE, EXTRALARGE }

    public static void main(String[] args) {
        EnumSet<Size> sizes = EnumSet.allOf(Size.class);
        System.out.println("EnumSet: " + sizes);
        boolean removed = sizes.remove(Size.MEDIUM);
        System.out.println("Is MEDIUM removed? " + removed);
        boolean cleared = sizes.removeAll(sizes);
        System.out.println("Are all elements removed? " + cleared);
    }
}

Output

EnumSet: [SMALL, MEDIUM, LARGE, EXTRALARGE]
Is MEDIUM removed? true
Are all elements removed? true

Additional Helper Methods

MethodDescription
copyOf()Creates a new EnumSet containing the same elements.
contains()Checks whether a specific constant is present.
isEmpty()Returns true if the set has no elements.
size()Returns the number of elements.
clear()Removes all elements.

Cloneable and Serializable

EnumSet implements Cloneable, enabling you to create a shallow copy via clone(). It also implements Serializable, allowing safe transmission over networks or persistence to disk.

Why Use EnumSet?

Because EnumSet stores its values as a bit vector, operations such as add, remove, and contains execute in constant time with minimal memory overhead. The JVM already knows the exact set of possible constants for a given enum, which makes EnumSet far more efficient than generic implementations like HashSet or TreeSet.

In practice, choose EnumSet whenever you need a high‑performance set of enum values.


Java

  1. Master Java Operators: Types, Syntax, & Practical Examples
  2. Java Comments: Types, Usage, and Best Practices
  3. Mastering Java if…else: Control Flow Explained
  4. Mastering Java's super Keyword: Advanced Usage & Practical Examples
  5. Mastering Java Interfaces: Concepts, Implementation, and Best Practices
  6. Master Java Enums: A Complete Guide to Enums & Enum Classes
  7. Java Enum Constructors Explained with Practical Example
  8. Mastering Java Try‑with‑Resources: Automatic Resource Management Explained
  9. Java Annotations Explained: Types, Placement, and Practical Examples
  10. Java EnumSet: Creation, Operations, and Performance Tips