Can EnumMap be considered a reasonable alternative to Java beans?

Curious if someone considered using EnumMap instead of Java beans, in particular “value objects” (without any behavior)? It seems to me that one advantage would be that the name of the “property” would be directly accessible from the fallback Enum, without the need for reflection, and therefore I assume that it will be faster.

+2
java javabeans value-objects enum-map
source share
5 answers

It may be a little faster than using reflection (I did not measure it, I did not find any indicators on Google); however, there are major disadvantages to this approach:

  • You are losing type safety. Instead of int getAge() and String getName() anyway Object get(MyEnum.FIELD_NAME) . This will provide some ugly code and runtime errors.

  • All javabean subtleties that we loved and enjoy (for example, annotations of the level of ownership) have disappeared.

  • Since you have NO BEHAVIOR, the applicability of this approach seems rather limited.

The bottom line is - if you really need the intended :-) performance improvement (which you will need to measure to prove that it exists), this can be a viable approach in very specific circumstances. Is this a viable alternative to javabeans in general? Most likely no.

+1
source share

A bean means mutability, hence setter methods. EnumMap is comparable in speed using a HashMap with integers as a key, but Keys are immutable. Beans and EnumMaps serve two different purposes. If all the keys are known during development and are guaranteed to never change, then using EnumMap will be fine.
Upgrading a bean is much simpler than changing an EnumMap backup with a much less chance of creating errors in the code.

+1
source share

I wrote a write class that maps keys to values ​​and works by delegating a fully synchronized EnumMap. The idea is that a record can receive new fields at runtime, while a Bean cannot. I came to the conclusion that with such flexibility comes a performance hit. This compares the Record class with a fully synchronized Bean. For 10 million operations:

 Record set(Thing, a) 458 ms Bean setThing(a) 278 ms Record get(Thing) 398 ms Bean getThing 248 ms 

So, you need to find out something in your data objects and write a class that models them statically. If you want new fields to be supplemented with your data at runtime, this will cost you.

+1
source share

I have not specified this before, but I am working with a ResultSet. Therefore, I want to give this answer for the sake of completeness.

Commons / BeanUtil "RowSetDynaClass" can be a happy environment between a redundant template associated with a particular beans and EnumMap restrictions

0
source share

I do not understand how you can remove class profiling with EnumMaps. Unless you have a generic enumeration with 20 odd reusable properties for each bean, you still invent an enumeration for use for each enumeration map, for example.

 public enum PersonDTOEnum { A, S, L; } 

Unlike

 class Person { int a; int s; String l; // getters + setters elided } 

Not to mention that now everything is a string.

0
source share

All Articles