9

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.

samwell
  • 2,757
  • 9
  • 33
  • 48
  • 1
    Isn't it good option to create a object with get/set and add that object HashTable/ArrayList (or) what ever the collection you are interested? If you add multiple data types, while retrieving if you don't know exact index and datatype, you will end up with classcastexception. – kosa Jul 19 '12 at 19:37
  • This sounds like a very fragile data structure. Could you describe what it is you're trying to achieve generally, so maybe someone can suggest a way to better structure your data? – Bobulous Jul 19 '12 at 20:06

5 Answers5

7

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.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • So then will I be able to retrieve it as the data type which it was stored as? – samwell Jul 19 '12 at 19:41
  • 2
    Not really, no -- not without a cast, and it'll still be finicky. You'd be _much_ better off using different maps for different types, or -- better yet -- just creating a class with fields with each of the different names, instead of having a `Map` where you sort of hope you make sure everything's of the right type. – Louis Wasserman Jul 19 '12 at 19:45
6
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.

The return type of get operation on such map is Object. Thus, it is developer's responsibility to handle type information correctly.

A good answer to the question as to what is the difference between Hashtable and HashMap is provided here.

Community
  • 1
  • 1
01es
  • 5,362
  • 1
  • 31
  • 40
4

You can have it store the general data type Object, though that will not allow primitive data types.

jrad
  • 3,172
  • 3
  • 22
  • 24
4

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.

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.

Russell Shingleton
  • 3,176
  • 1
  • 21
  • 29
0

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.

Simulant
  • 19,190
  • 8
  • 63
  • 98