Home Ask Login Register

Developers Planet

Your answer is one click away!

Ramesh February 2016

Java - Model class with so many properties vs one single HashMap<key,value>

I am trying to read multiple text files ( each file having different layouts / schema's).

Currently my Model class have more than 40 properties and its kind of hard coded.

I am planning to use HashMap<>, so everytime any new file comes, i can read the file without changing the model class by adding more properties.

But is this correct way to do it?

Please advise.


Michael Gantman February 2016

Both approaches have their pros and cons. Map gives you flexibility for schemas unknown in advance but could become nightmare in maintenance, and it is not error safe - i.e. your types are not checked. The bean with predefined properties is the opposite. In general it is better option but you will have to change it every time the schema changes. You can have a combined approach. Say you have a set of properties that will always be there. And then you may or may not have some other properties that are unknown in advance. So you have a bean with predefined setters and getters and add another setter and getter for a Map property that will deal with unknown properties. I hope this helps

Post Status

Asked in February 2016
Viewed 3,166 times
Voted 10
Answered 1 times


Leave an answer

Quote of the day: live life