java 有没有办法在单个哈希表变量中存储多种数据类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11568013/
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
Is there a way to store multiple data types in a single hashtable variable?
提问by samwell
I have a Hashtable
which has a key of String
and the value as String
, but I've reached a point in my project where I need to be able to store multiple different data types. For example, I'm going to need to store int
, String
, Date
, etc, all in one Hashtable
.
我有一个Hashtable
键为 of String
,值为 as String
,但我在我的项目中已经达到了需要能够存储多种不同数据类型的地步。例如,我将需要存储int
,String
,Date
,等等,都在同一个Hashtable
。
采纳答案by pb2q
HashTable
or any Collection Map
can handle this, except for int
and other primitive types: you can't store primitive types, only Object
references. int
will need to be wrapped as a Integer
object.
HashTable
或者任何 CollectionMap
都可以处理这个,除了int
和其他基本类型:你不能存储基本类型,只能存储Object
引用。int
将需要包装为一个Integer
对象。
回答by 01es
Map<String, Object> map = new HashMap<String, Object>()
This gives you a map with keys of type String and values of type Object, which basically means any descendant of type Object (Date, Integer, String etc.). Other answers correctly point to the fact that instead of using primitives such as int, boolean it is required to use their counterparts Integer, Boolean etc.
这为您提供了一个包含 String 类型键和 Object 类型值的映射,这基本上意味着 Object 类型(日期、整数、字符串等)的任何后代。其他答案正确地指出这样一个事实,即不使用 int、boolean 等原语,而是需要使用它们的对应物 Integer、Boolean 等。
The return type of get
operation on such map is Object
. Thus, it is developer's responsibility to handle type information correctly.
get
这种地图上操作的返回类型是Object
。因此,正确处理类型信息是开发人员的责任。
A good answer to the question as to what is the difference between Hashtable and HashMap is provided here.
回答by Russell Shingleton
While this is possible, it's generally not a good idea. More often than not this leads to type casting exceptions and problems.
虽然这是可能的,但通常不是一个好主意。这通常会导致类型转换异常和问题。
The HashTable can be set to store generic Objects as opposed to specific class types, however the type conversion when retrieving them does not happen automatically.
HashTable 可以设置为存储通用对象而不是特定的类类型,但是检索它们时的类型转换不会自动发生。
In order to get the object back out of the collection, some form of type checking routines will have to be developed.
为了从集合中取出对象,必须开发某种形式的类型检查例程。
You're better off creating a separate collection for each class type you want to store.
最好为要存储的每个类类型创建一个单独的集合。
PS: I would also recommend using a HashMap instead of HashTable. HashTable has been deprecated.
PS:我还建议使用 HashMap 而不是 HashTable。HashTable 已被弃用。
回答by jrad
You can have it store the general data type Object
, though that will not allow primitive data types.
您可以让它存储通用数据类型Object
,但不允许原始数据类型。
回答by Simulant
Change your HashTable to Hashtable<String, Object>
and when you want to store an int
you need to cast it first(or use autocast) to an Integer
. After getting your value from the table you can determ your type by if(value instanceof String)
and so on for any type.
将您的 HashTable 更改为Hashtable<String, Object>
当您想要存储时,int
您需要先将其转换(或使用自动转换)到Integer
. 从表中获取您的值后,您可以if(value instanceof String)
根据任何类型等来确定您的类型。