Ranjan Kumar
Ranjan Kumar

Reputation: 107

How to use a single map container to put multiple type of Value objects

For example.

I have multiple classes as: Inventory , Product , Sales etc.

Now my requirement is to put all above types in a single map container. I don't want to create multiple maps for putting above each object values.

Upvotes: 0

Views: 471

Answers (2)

hmir
hmir

Reputation: 1413

Set the value type as Object:

Map</*key goes here*/, Object> myMap = new Map</*key goes here*/, Object>();

Or, if Inventory, Product, Sales, and whatever else is going into the map share a superclass or implement the same interface, then set the value type as that:

Map</*key goes here*/, Superclass> myMap = new Map</*key goes here*/, Superclass>();

Upvotes: 1

gilleain
gilleain

Reputation: 641

If all your classes can implement the same interface, then you can store them all in the same container. Otherwise you would have to use Object...

More generally - why do you not want to use multiple maps?

Upvotes: 1

Related Questions