Java list all values in enum

Can I add and remove elements of enumeration at runtime in Java

It is possible to add and remove elements from an enum in Java at runtime? For example, could I read in the labels and constructor arguments of an enum from a file? @saua, it’s just a question of whether it can be done out of interest really. I was hoping there’d be some neat way of altering the running bytecode, maybe using BCEL or something. I’ve also followed up with this question because I realised I wasn’t totally sure when an enum should be used. I’m pretty convinced that the right answer would be to use a collection that ensured uniqueness instead of an enum if I want to be able to alter the contents safely at runtime.

7 Answers 7

No, enums are supposed to be a complete static enumeration.

At compile time, you might want to generate your enum .java file from another source file of some sort. You could even create a .class file like this.

In some cases you might want a set of standard values but allow extension. The usual way to do this is have an interface for the interface and an enum that implements that interface for the standard values. Of course, you lose the ability to switch when you only have a reference to the interface .

Would you know where can I find a good example of an enum implementing such interface? I will be interested in such functionality.

Читайте также:  Радиус

@megalucio I think I was thinking of java.nio.file.StandardOpenOption docs.oracle.com/javase/8/docs/api/java/nio/file/… but there’s also java.net.StandardProtocolFamily and javax.tools.StandardLocation .

Behind the curtain, enums are POJOs with a private constructor and a bunch of public static final values of the enum’s type (see here for an example). In fact, up until Java5, it was considered best-practice to build your own enumeration this way, and Java5 introduced the enum keyword as a shorthand. See the source for Enum to learn more.

So it should be no problem to write your own ‘TypeSafeEnum’ with a public static final array of constants, that are read by the constructor or passed to it.

Also, do yourself a favor and override equals , hashCode and toString , and if possible create a values method

The question is how to use such a dynamic enumeration. you can’t read the value «PI=3.14» from a file to create enum MathConstants and then go ahead and use MathConstants.PI wherever you want.

Is that true? As the instance on the end of the reference is immutable, I’m not sure how you could actually change it. is it just crossed wires over whether it’s an array instance or not?

Collections and Arrays are both mutable Objects (and «Objects»). Having a «final» array just means you’re not going to resize or otherwise reassign the array, but it still lets you replace values that already fit in its bounds.

I needed to do something like this (for unit testing purposes), and I came across this — the EnumBuster: http://www.javaspecialists.eu/archive/Issue161.html

It allows enum values to be added, removed and restored.

Edit: I’ve only just started using this, and found that there’s some slight changes needed for java 1.5, which I’m currently stuck with:

  • Add array copyOf static helper methods (e.g. take these 1.6 versions: http://www.docjar.com/html/api/java/util/Arrays.java.html)
  • Change EnumBuster.undoStack to a Stack
    • In undo(), change undoStack.poll() to undoStack.isEmpty() ? null : undoStack.pop();

    With modern versions of Java, you need to change a few things as documented in the revised article: javaspecialists.eu/archive/…

    The approach I took was to save the values and the names of the enumeration externally, and the end goal was to be able to write code that looked as close to a language enum as possible.

    I wanted my solution to look like this:

    enum HatType < BASEBALL, BRIMLESS, INDIANA_JONES >HatType mine = HatType.BASEBALL; // prints "BASEBALL" System.out.println(mine.toString()); // prints true System.out.println(mine.equals(HatType.BASEBALL)); 

    And I ended up with something like this:

    // in a file somewhere: // 1 --> BASEBALL // 2 --> BRIMLESS // 3 --> INDIANA_JONES HatDynamicEnum hats = HatEnumRepository.retrieve(); HatEnumValue mine = hats.valueOf("BASEBALL"); // prints "BASEBALL" System.out.println(mine.toString()); // prints true System.out.println(mine.equals(hats.valueOf("BASEBALL")); 

    Since my requirements were that it had to be possible to add members to the enum at run-time, I also implemented that functionality:

    hats.addEnum("BATTING_PRACTICE"); HatEnumRepository.storeEnum(hats); hats = HatEnumRepository.retrieve(); HatEnumValue justArrived = hats.valueOf("BATTING_PRACTICE"); // file now reads: // 1 --> BASEBALL // 2 --> BRIMLESS // 3 --> INDIANA_JONES // 4 --> BATTING_PRACTICE 

    I dubbed it the Dynamic Enumeration «pattern», and you read about the original design and its revised edition.

    The difference between the two is that the revised edition was designed after I really started to grok OO and DDD. The first one I designed when I was still writing nominally procedural DDD, under time pressure no less.

    Doesn’t this break best practices for Java which assumes that Enumerators are static and final? It’s almost like you’re using a local file as a database of constant values that must be read fresh to guarantee latest as previously read data could be stale.

    You can load a Java class from source at runtime. (Using JCI, BeanShell or JavaCompiler)

    This would allow you to change the Enum values as you wish.

    Note: this wouldn’t change any classes which referred to these enums so this might not be very useful in reality.

    A working example in widespread use is in modded Minecraft. See EnumHelper.addEnum() methods on Github

    However, note that in rare situations practical experience has shown that adding Enum members can lead to some issues with the JVM optimiser. The exact issues may vary with different JVMs. But broadly it seems the optimiser may assume that some internal fields of an Enum, specifically the size of the Enum’s .values() array, will not change. See issue discussion. The recommended solution there is not to make .values() a hotspot for the optimiser. So if adding to an Enum’s members at runtime, it should be done once and once only when the application is initialised, and then the result of .values() should be cached to avoid making it a hotspot.

    The way the optimiser works and the way it detects hotspots is obscure and may vary between different JVMs and different builds of the JVM. If you don’t want to take the risk of this type of issue in production code, then don’t change Enums at runtime.

    You could try to assign properties to the ENUM you’re trying to create and statically contruct it by using a loaded properties file. Big hack, but it works 🙂

    Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

    Linked

    Hot Network Questions

    Subscribe to RSS

    To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

    Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.24.43543

    By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

    Источник

    List of enum values in java

    If you do not care about the order, use EnumSet , an implementation of Set .

    enum Animal < DOG , CAT , BIRD , BAT ; >Set flyingAnimals = EnumSet.of( BIRD , BAT ); Set featheredFlyingAnimals = flyingAnimals.clone().remove( BAT ) ; 

    If you care about order, use a List implementation such as ArrayList . For example, we can create a list of a person’s preference in choosing a pet, in the order of their most preferred.

    List < Animal >favoritePets = new ArrayList<>() ; favoritePets.add( CAT ) ; // This person prefers cats over dogs… favoritePets.add( DOG ) ; // …but would accept either. // This person would not accept a bird nor a bat. 

    For a non-modifiable ordered list, use List.of .

    List < Animal >favoritePets = List.of( CAT , DOG ) ; // This person prefers cats over dogs, but would accept either. This person would not accept a bird nor a bat. 

    Details

    The Answer ( EnumSet ) by Amit Deshpande and the Answer ( .values ) by Marko Topolnik are both correct. Here is a bit more info.

    Enum.values

    The .values() method is an implicitly declared method on Enum , added by the compiler. It produces a crude array rather than a Collection . Certainly usable.

    Special note about documentation: Being unusual as an implicitly declared method, the .values() method is not listed among the methods on the Enum class. The method is defined in the Java Language Specification, and is mentioned in the doc for Enum.valueOf .

    EnumSet – Fast & Small

    The upsides to EnumSet include:

    Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based «bit flags.» Even bulk operations (such as containsAll and retainAll) should run very quickly if their argument is also an enum set.

    Make an EnumSet in one line.

    Set allAnimals = EnumSet.allOf( Animal.class ); 
    System.out.println( "allAnimals : " + allAnimals ); 

    Make a set from a subset of the enum objects.

    Set flyingAnimals = EnumSet.of( BIRD , BAT ); 

    Look at the class doc to see many ways to manipulate the collection including adding or removing elements.

    Set featheredFlyingAnimals = EnumSet.copyOf( flyingAnimals ).remove( BAT ); 

    Natural Order

    The doc promises the Iterator for EnumSet is in natural order, the order in which the values of the enum were originally declared.

    The iterator returned by the iterator method traverses the elements in their natural order (the order in which the enum constants are declared).

    Frankly, given this promise, I’m confused why this is not a SortedSet . But, oh well, good enough. We can create a List from the Set if desired. Pass any Collection to constructor of ArrayList and that collection’s Iterator is automatically called on your behalf.

    List list = new ArrayList<>( allAnimals ); 
    System.out.println("list : " + list ); 

    In Java 10 and later, you can conveniently create a non-modifiable List by passing the EnumSet . The order of the new list will be in the iterator order of the EnumSet . The iterator order of an EnumSet is the order in which the element objects of the enum were defined on that enum.

    List < Animal >nonModList = List.copyOf( allAnimals ) ; // Or pass Animals.values() 
    new ArrayList(Arrays.asList(MyEnum.values())); 

    Yes it is definitely possible, but you will have to do

    You can then add elements to al : al.add(ONE) or al.add(TWO) .

    Источник

Оцените статью