Notification texts go here Contact Us Buy Now!

Why doesn't Java 21's EnumSet implement the new SequencedSet interface?

There isn't a convincing technical argument against EnumSet implementing SequencedSet.

Implementing reversed() wouldn't be a major issue.

So why doesn't it implement it?

There is no definitive answer, but it's likely due to a combination of factors:

  • Oversight
  • Lack of time
  • Uncertain value of the effort

Regardless, an OpenJDK bug report exists for this issue.

Fundamentals

EnumSet could implement SequencedSet or even NavigableSet because:

  • Enum values have a defined ordering
  • EnumSet's iterator iterates in order

The challenge lies in determining the type of the reversed view, as SequencedSet<E extends Enum<E>> would drop the "EnumSet"-ness from the return type.

Modifying EnumSet to allow reverse iteration could mitigate this issue, but it might cause problems for existing code that relies on forward iteration.

Usefulness

The primary use case for EnumSet is as an array of flags, where individual flags are set and queried. Adding SequencedSet doesn't provide additional value in this context.

Another use case is as a set of commands or functions, where enum values represent commands and are processed in order. However, this use case is already mostly supported by forward iteration.

While it's possible that someone might want to operate on the first or last elements, or iterate in reverse, these use cases seem rare.

Cost

Adding a reversed view isn't difficult, but it involves creating wrapper classes and additional testing and specification work.

Implementing NavigableSet's subset views is even more work.

Completeness

Given that enums are totally ordered, it could be argued that EnumSet should implement the best possible interface for completeness' sake.

However, completeness must be balanced against time and effort constraints.

Priorities

The cost-benefit tradeoff of implementing SequencedSet is moderate, but the benefit is relatively low.

Additionally, there are other competing priorities, such as implementing an unmodifiable EnumSet, which may offer greater value.

Summary

The decision not to implement SequencedSet in EnumSet is likely due to a combination of factors, including the lack of a compelling use case, the cost of implementation, and competing priorities.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.