java 类对象 vs Hashmap
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10258071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class Object vs Hashmap
提问by abhi
Is it good to use hashmap instead of using the object class...... Using Hashmap....
用hashmap代替对象类好不好……使用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 ...
请在应用程序运行状况、内存要求等方面告诉我...
回答by dasblinkenlight
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.
总而言之,在您决定使用哈希图的灵活性之前,您应该确定您的设计是否真的需要所有灵活性。有时,答案是“是”,有时是“否”;对此没有“一刀切”的解决方案。
回答by JB Nizet
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.
一个对象有字段(数据)和方法(行为)。如果您的数据包含在一组固定的单元格(A、B 和 C)中,那么一定要使用对象。
Java is an OO object, and OO design, encapsulation etc. are there to help you build robust, maintainable and fast programs.
Java 是一个面向对象的对象,面向对象的设计、封装等可以帮助您构建健壮、可维护和快速的程序。
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.
当您必须关联可变数量的键和值时,Map 很有用。但它只是一个数据结构,不允许你封装额外的行为。
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?
例如,您的对象中可能有一个 getAAndB() 方法,该方法返回与 B 连接的 A。或者您可能有转换或查询字段的方法。或者您可以将 ABC 实例传递给使用它们的其他对象。使用具有明确定义方法的对象 ABD 比使用Map<String, String>
. 地图的关键是什么?他们的价值观是什么?它在哪里记录?如果您想更改密钥怎么办?您将如何检测代码中使用这些键的所有位置?
回答by Ushox
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 yesor a definitive nowithout knowing the larger context.
您应该将其视为性能之前的“设计”问题。无需为了良好的设计而进行前期过早优化。因此,问题是:“您是否需要通过中间集合来填充域对象 ABC?” 在大多数情况下,我不会这样做,但在不了解更大背景的情况下,很难说明确的“是”或“明确的否”。
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.
更新:30-40K:记录数与 Object 与 HashMap 的比较无关,因为它们将在循环中处理(免责声明:与设计无关,与性能无关)。但是,电子表格中的列数很重要,因为这将直接反映为对象中的属性数。
If this is just a data migrationor data transferexercise 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.
如果这只是数据迁移或数据传输练习,那么我会采用 HashMap 方法。假设 ABC 将是一个没有行为的短暂的、一次性的数据容器对象,则无需创建它。然后我会测试系统的性能,如果它不满足验收标准,那么我会对其进行分析并仅在必要时对其进行优化。