When to use Enum or Collection in Java

前端 未结 6 1721
醉梦人生
醉梦人生 2020-12-14 02:03

Under what circumstances is an enum more appropriate than, for example, a Collection that guarantees unique elements (an implementer of java.util.Set, I guess...)?

(

6条回答
  •  Happy的楠姐
    2020-12-14 02:11

    Basically when it's a well-defined, fixed set of values which are known at compile-time.

    You can use an enum as a set very easily (with EnumSet) and it allows you to define behaviour, reference the elements by name, switch on them etc.

提交回复
热议问题