59

Is there a builtin way to get the number of items of an Enum with something like Myenum.length,

Or do I have to implement myself a function int size() hardcording the number of element?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
  • 2
    Are you talking about enums or an Enumeration ? These are not the same. – Cyrille Ka Jul 05 '13 at 15:14
  • Was talking about enums. Problem is that in french enums is a shorthand for "enumeration".(as in english I tend to think). so wasn't speaking of the interface. – AdrieanKhisbe Jul 05 '13 at 15:27
  • Is it OK that I added the Kotlin tag? I Googled a Kotlin solution and ended up here. – Mahozad Sep 27 '21 at 08:19
  • Refer to [this related post](https://stackoverflow.com/q/1741708/8583692). – Mahozad Sep 27 '21 at 08:23
  • Also see [this youtrack issue](https://youtrack.jetbrains.com/issue/KT-48872) that proposes a performant replacement for `Enum.values()` in Kotiln. – Mahozad Nov 06 '21 at 12:10

7 Answers7

88

Yes you can use the Enum.values() method to get an array of Enum values then use the length property.

public class Main {
    enum WORKDAYS { Monday, Tuesday, Wednesday, Thursday, Friday; }

    public static void main(String[] args) {
        System.out.println(WORKDAYS.values().length);
        // prints 5
    }
}

http://ideone.com/zMB6pG

user207421
  • 305,947
  • 44
  • 307
  • 483
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • 7
    `Enum.values()` allocates a new array every time so it's not recommended for high call rate (i.e. for-loop condition). Cache the value if you need to call it very often. – Mark Jeronimus Oct 21 '19 at 13:46
  • 2
    @MarkJeronimus Even without calling it often, this is still bad practice. Allocating an array just for the length property is silly, even if only done once. – Hatefiend Feb 04 '21 at 08:35
  • 7
    @Hatefiend so what do you suggest? – Bouh May 05 '21 at 23:42
13

You can get the length by using Myenum.values().length

The Enum.values() returns an array of all the enum constants. You can use the length variable of this array to get the number of enum constants.

Assuming you have the following enum:

public enum Color
{
    BLACK,WHITE,BLUE,GREEN,RED
}

The following statement will assign 5 to size:

int size = Color.values().length;
Rahul Bobhate
  • 4,892
  • 3
  • 25
  • 48
8

To avoid calling values() method each time:

public class EnumTest {

    enum WORKDAYS {
       Monday,
       Tuesday,
       Wednesday,
       Thursday,
       Friday;

       public static final int size;
       static {
          size = values().length;
       }
   }


   public static void main(String[] args) {
      System.out.println(WORKDAYS.size); // 5
   }
}
5

The enum.values() method is added by the Java complier and is not mentioned in the APIs.

Where is the documentation for the values() method of Enum?

Community
  • 1
  • 1
jaamit
  • 393
  • 3
  • 11
3

MyEnum.values() returns the enum constants as an array.

So you can use:

int size = MyEnum.values().length
Puce
  • 37,247
  • 13
  • 80
  • 152
0

I searched for a Kotlin answer and this question popped up. So, here is my answer.

enum class MyEnum { RED, GREEN, BLUE }
MyEnum.values().size // 3

This is another way to do it:

inline fun <reified T : Enum<T>> sizeOf() = enumValues<T>().size
sizeOf<MyEnum>() // 3

Thanks to Miha_x64 for this answer.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
  • 1
    See [this Kotlin issue](https://youtrack.jetbrains.com/issue/KT-48872) aiming to replace `Enum.values()` with a more modern and performant alternative. – Mahozad Dec 03 '21 at 11:16
0

In reply to the answer of Yevhen Tkachenko it can be further optimized when using the enum values array, so the compiler calls values() only once:

public class EnumTest {

    enum WORKDAYS {
       Monday,
       Tuesday,
       Wednesday,
       Thursday,
       Friday;

       public static final WORKDAYS[] values = WORKDAYS.values();
   }

   public static void main(String[] args) {
      System.out.println(WORKDAYS.values.length); // 5
      for (int i = 0; i < WORKDAYS.values.length; i++)
         System.out.println(WORKDAYS.values[i].name());
   }
}
nix
  • 233
  • 3
  • 5