How to determine lowest/highest index of enum in java?

enums, java

Solution

You'll have to iterate over the enum set:

for (Color p : Color.values()) {
    // keep track of min "index"
}

Remember that an enum is essentially collection of predefined object instances. RED(100) is calling the Color(int value) constructor. That said, I could make a color enum with values defined like this:

RED("best", 14, 3.33546)

Hence, the logic for finding the minimum "index" will be different case by case.

Problem

Suppose there is the enum declaration somewhere in code: ``` enum Colors { RED(100), BLUE(200); } ``` Can I get the lowest/highest index value for that particular enum type presuming I am not aware of the declaration? Is it possible in java? Example: ``` int lowIndex = Colors.minIndex(); // should return 100 ``` Thanks everyone. So there are no implicit methods to query for min/max defined integer value. I'll have to iterate through the enum values and determine it from there as you have described.

Original source