Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How can I find the maximum of two enum values?

ptoyeOct 5 2014 — edited Oct 7 2014

I have a set of enum values (let's call then ONE, TWO, THREE.....). I want to find the larger of two of them. But max(ONE,THREE) gives a compile error as MAX isn't defined for type-safe enums. Fair enough.

I also agree that one shouldn't be able to use arithmetic functions on enums.

But as Enum implements Comparable, one can write a function which implements max and min, rather inefficiently I assume.

Is there a better way of getting the max/min of an enum? And if not, can the Java team be persuaded to implement it?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 4 2014
Added on Oct 5 2014
6 comments
4,287 views