Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to store multiple data types in a single hashtable variable?

Tags:

java

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.

like image 545
samwell Avatar asked Jul 19 '12 19:07

samwell


People also ask

Can HashTable hold multiple types?

Object Types in HashTables The keys and values in a hash table can have any . NET object type, and a single hash table can have keys and values of multiple types.

Can HashTable store different data types?

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.

Can a variable have multiple data types?

Variables can store data of different types, and different data types can do different things.

How do you store multiple data types?

You can create an array with elements of different data types when declare the array as Object. Since System. Object is the base class of all other types, an item in an array of Objects can have a reference to any other type of object.


1 Answers

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.

like image 117
pb2q Avatar answered Sep 22 '22 14:09

pb2q