Looking for Java Enum types from code values?

We use code values โ€‹โ€‹in our database and Enums in Java. When querying the database, we need to take the code value and get an instance of Enum.

Is it too redundant to use a HashMap to avoid iteration? What would you do? Is there an easier way?

public enum SomeEnum { TYPE_A(2000), TYPE_B(2001); private int codeValue; private static HashMap<Integer, SomeEnum> codeValueMap = new HashMap<Integer, SomeEnum>(2); static { for (SomeEnum type : SomeEnum.values()) { codeValueMap.put(type.codeValue, type); } } //constructor and getCodeValue left out public static SomeEnum getInstanceFromCodeValue(int codeValue) { return codeValueMap.get(codeValue); } } 
+4
source share
5 answers

This is the approach I would take to solve this particular problem. I do not see anything wrong with this from a design point of view, it is intuitive, effective and (as far as I see) does exactly what it should.

The only reasonable approach I can think of is to have a map in a separate class, and then call this class to update the map from the SomeEnum constructor. Depending on the use case, this separation may be useful - but, if it does not have a particular advantage, I would take your approach and encapsulate everything inside the enumeration itself.

+4
source

Thanks, I think my main problem is memory usage, and if it's worth it.

If an enum value has thousands of values, memory usage will be trivial. (And if it has thousands of meanings, then using iteration to search will be a serious killer of performance.)

This is a reasonable use of memory, IMO.

Perhaps I already thought about that.

Perhaps you are.

+1
source

I think that in this case we cannot avoid iteration. This either HashMap does this, or we wrote our own iteration code. If performance really matters, perhaps you can try the binary tree approach.

+1
source

It's good. Donโ€™t worry about tiny differences in performance.

You might think that if there are only two instances for listing, as in your example, the trivial iteration code will be faster:

 public static SomeEnum getInstanceFromCodeValue(int codeValue) { for (SomeEnum type : SomeEnum.values()) { if(type.codeValue == codeValue) return type; } } 

But there is a hidden cost, quite expensive if we really care about performance at that level. This is a fix, but you need to see it first :)

+1
source

If your enumeration space is dense, that is, there are not many unused values, you can use the toString () and valueOf () methods. Name your values โ€‹โ€‹the common string prefix, then attach the prefix before using valueOf () and split it after using toString (). This has the disadvantage that you will have to convert to a numerical value if it is stored in your database.

Alternatively, you can add common conversion methods and assign the value of your database to a specific enumeration value.

Both of these methods have the advantage of using the design of enumeration classes.

There is a lot of good bending information about enums (and Java in general) at http://mindprod.com/jgloss/enum.html .

Although, if you do the right job, there is nothing wrong.

+1
source

Source: https://habr.com/ru/post/1340723/


All Articles