Reputation: 59
for take values from a class where there are only primitive fields (no methods) as a list of key - value do you prefer use reflection (with a dynamic iteration) or a specific method that return an HashMap? What do you prefer?
At this moment there are fifty classes or so but probably in the future they can grow to another twenty or thirty (in regime)
I like reflection but in this case probably is not necessary?
Upvotes: 1
Views: 63
Reputation: 727127
The reflection-based approach is cleaner, because it does not require your classes to implement additional methods, and you would not be liable to maintain these methods when new fields are added to existing classes.
A HashMap
-based approach does not buy you much in terms of performance, because primitives must be wrapped in objects to be inserted into the map - the same way they would be wrapped to be returned from the reflection-based calls.
Upvotes: 2