30

Is it good to use hashmap instead of using the object class...... Using Hashmap....

Map<String, String> cellMap = new HashMap<String, String>();
int j = 0;
while (cellIter.hasNext()) 
{
   HSSFCell myCell = (HSSFCell) cellIter.next();
   cellMap.put(columnMap[j], myCell.toString());
   j++;
}

And using object class.....

ABC abc= new ABC(); 
abc.setA(myRow.getCell(0).toString());
abc.setB(myRow.getCell(1).toString());
abc.setC(myRow.getCell(2).toString());

Please tell me in the context of application health, memory requirement etc ...

5
  • 1
    Actually i am reading an xls file which will contain lakhs of records my Boss has asked me to update the oracle by reading data from xls file ONE BY ONE only using object class's getters and setters in place of using a hashmap in which map all the data and then pass it to oracle. Commented Apr 21, 2012 at 10:36
  • 3
    For non-Indians, 1 lakh = 100K :-) Commented Apr 21, 2012 at 10:40
  • If you use a HashMap, how do you get the "type" of the value when you store it in Oracle? (assuming that your spreadsheet contains values of multiple types such as string, numeric values etc) Commented Apr 21, 2012 at 10:41
  • First map columns_name and their values using hashmap as String, String ...... Then Just typecast them... Commented Apr 21, 2012 at 10:44
  • I have done it this way... I was able to change date, string, numerics etc.. Commented Apr 21, 2012 at 10:45

3 Answers 3

43

This depends a lot on what you are trying to achieve: for flexibility, hash map is better. But the flexibility comes at a price: hash map is also larger and slower than a class with the identical number of strongly-typed fields.

  • Hash map has larger memory footprint than a class with identical number of fields
  • Hash map forces boxing on primitives
  • Hash map is slower to create and access

There is also an impact on readability: when you business logic is specific to a class with a fixed number of fields, a special-purpose class clearly wins; when the fields are configured dynamically, hash table is your only option. You could also have a hybrid design, when an object uses a hash map for its storage internally, presents nicely named fields externally, and exposes semantics to add more "fields" as you go.

To summarize, before you decide to go with a hash map for its flexibility, you should decide if you really need all that flexibility in your design. Sometimes, the answer is "yes", and sometimes it is "no"; there is no "one size fits all" solution to this.

Sign up to request clarification or add additional context in comments.

Comments

7

An object has fields (data) and methods (behaviour). If your data consists in a fixed set of cells (A, B and C), then definitely use an object.

Java is an OO object, and OO design, encapsulation etc. are there to help you build robust, maintainable and fast programs.

A Map is useful when you must associate a variable number of keys and values. But it's simply a data structure, and doesn't allow you to encapsulate additional behavior.

For example, you might have a getAAndB() method in your object that returns A concatenated with B. Or you might have methods to transform or query the fields. Or you could pass ABC instances to other objects that make use of them. Using an object ABD with well-defined methods is much easier than using a Map<String, String>. What are the keys of the map? What are their values? Where is it documented? What if you want to change the keys? How will you detect all the places in the code where these keys are used?

2 Comments

I have fixed data to read with fixed no of columns at fixed position ..... Keys to every values is also fixed as u can see i m passing a fixed string array as the key in the hashmap...and I dont want to change the keys.... I knows where all I will be using these keys.... the things is if it fails any of the things above Simply THROW AN ERROR
Then use an object. If you're not sure about the validity of the Excel file, then validate it before or while reading it and creating instances of your objects (check that mandatory cells are populated, for example). But (supposing the cells represent fields of persons), it's much more readable to deal with Person objects having a first name, a middle name and a last name than dealing with a Map<String, String>.
4

You should see this as a "design" issue before performance. No need to do upfront premature optimization in favour of good design. So, the question is: "do you need to go through an intermediary collection to populate your domain object ABC?" In most cases I wouldn't do it but it's hard to say a definitive yes or a definitive no without knowing the larger context.

UPDATE: 30-40K: Number of records is irrelevant for the Object vs HashMap comparison because they're going to be handled in a loop (disclaimer: irrelevant in terms of design not in terms of performance). However the number of columns in your spreadsheet is important as this is going to be reflected directly as the number of attributes in your object.

If this is just a data migration or data transfer exercise then I'd go with the HashMap approach. Assuming that ABC will be a short-lived, throwaway data container object without behaviour, there's no need to create it. Then I'd test the performance of the system and if it doesn't satisfy the acceptance criteria then I'd profile it and optimize it only if necessary.

2 Comments

Hmm... If the question is "should I use a HashMap attribute instead of A, B and C attributes in my domain object, I would NOT use a HashMap. A, B, C are your object's attributes, they shouldn't be merely stored in a HashMap.
It is actually like reading and setting the data using hashmap or a class...and then retrieving from it ......

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.