Store an ordering of Enums in Java
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Enums in Java: Storing and Ordering
Enums in Java are a special kind of Java class used to define collections of constants. They are implicitly final and static, meaning you cannot create subclasses of enums and can't modify enums instance fields directly. An enum class is a specialized class that is a full-fledged member of the Java type system. This article delves into the nuances of storing and ordering enums, enhancing your understanding with technical explanations, examples, and a summary table.
Basics of Enums
Enums are often used when you have a fixed set of related constants. Consider the following simple enum declaration:
Key Characteristics of Enums:
- Type Safety: Enums provide type safety and a unique namespace for their constants.
- Implicit
staticandfinal: Enum constants are implicitlypublic,static, andfinal. - Methods and Fields: Enums can have methods and fields just like regular classes.
- Enums are Comparable: Every enum in Java has a built-in
ordinal()method, which returns the position of the enum constant in the enum declaration, starting from 0.
Storing Enums in Java
Enums can store additional information using fields. Let's consider an example where each day of the week is associated with an activity:
Here, each day is associated with an activity. You can retrieve this information with the getActivity() method.
Ordering Enums
Ordering in enums can be achieved by utilizing their natural order defined by the order they are declared in. Java provides an ordinal() method that allows you to determine an enum constant's ordinal position. Enum constants can also be compared using the compareTo() method, which compares based on ordinal values.
Example of Ordering with ordinal():
In this example, THURSDAY will have an ordinal value of 4.
Example of Ordering with compareTo():
Sorting Enums
In scenarios where you need to sort an array or list of enums, using Arrays.sort() or Collections.sort() is straightforward due to the natural ordering provided by compareTo():
Custom Ordering of Enums
Sometimes, the natural order is not suitable, and a custom order is required. This can be done by implementing Comparator:
In this case, daysList will be sorted based on the associated activity.
Summary Table
| Feature | Description | Example |
| Type Safety | Provides type safety and unique namespace | Day.SUNDAY |
| Implicit Properties | Enum constants are public, static, final | - |
| Methods & Fields | Enums can have methods and fields | see Day example |
| Natural Ordering | Defined by declaration order (using ordinal()) | MONDAY.ordinal() returns 1 |
| Comparison | Supports compareTo(), compares ordinal() values | MONDAY.compareTo(WEDNESDAY) |
| Sorting | Uses natural ordering by default | Arrays.sort(Day.values()) |
| Custom Ordering | Implement with Comparator | Compare by activity |
| Storage with Additional Data | Enums can store additional info using fields | private final String activity in Day enum |
Enums, by their design, ensure robustness and clarity. They provide a structured way to represent fixed sets of constants. When you need to store additional states or leverage custom orderings, enums offer the flexibility to accommodate these needs efficiently. Understanding how to manipulate and use enums effectively can lead to cleaner and more maintainable codebases.

