I have a POJO that I want to convert into a HashMap. Now in the past, I've done the following.
And it works, but it's a pain to create and needs to be updated any time a field changes.
What I'd like to be able to do is loop through all of the get methods, automatically placing them in as key/value pairs. I know you can do this with reflection, but is there an easier, less resource intensive way to do it?
Yeah, I didn't think it was possible, but I thought I'd ask. I was hoping to avoid a reflection performance hit.
posted 9 years ago
Rob Prime wrote:But that's not without using reflection.
True (and reflection sucks to debug/maintain), but the implementation is pretty straightforward. One thing you could do is implement a toHashMap() method which builds a HashMap of the current object. Downside of that is that you'd have to implement it in every class you want that functionality and consistently update it (something you already mentioned). If you've written a bunch of code already then it would suck, but if you're just starting out it wouldn't be to bad. Just like unit testing... update the class update the test/HashMap method.