类对象与Hashmap [英] Class Object vs Hashmap

查看:146
本文介绍了类对象与Hashmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用hashmap而不是使用对象类是好的...
使用Hashmap ....

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 ...

推荐答案

这取决于你想实现什么:为了灵活性,哈希映射更好。但是灵活性有一个代价:哈希映射也比具有相同数量的强类型字段的类更大和更慢。

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.


  • 哈希映射比具有相同数目字段的类具有更大的内存占用

  • 哈希映射强制对原语进行加载

  • 哈希映射创建和访问速度较慢

对可读性也有影响:当业务逻辑特定于具有固定数量字段的类时,目的类明显胜利;当字段动态配置时,哈希表是您唯一的选项。你也可以有一个混合设计,当一个对象在内部使用哈希映射来存储它的存储,在外部显示很好的命名字段,并且暴露语义以在你去时添加更多的字段。

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.

这篇关于类对象与Hashmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆