dcsimg
I need a tricky Map
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   zeus_zeus
Posted On:   Monday, October 29, 2001 10:17 PM

Situation is like this, I have a class hierarchy that describes objects, each objects adds some properties of its own. I store this properties in a map. Now the map is a member of the base class. Now the problem: The set of properties for each class is known and fixed (its like it were members in the descendant class) and its same for all instances of the class (and there are as many as 1000s and more). If I could have a map where the keys are static and the values are not it would exactly match my requirement, as it would function as a map, though it would cost much less memory. I hope the question is clear, anyway I'll give an example: class A { protected MyMap properties; } class B extends class A    More>>

Situation is like this, I have a class hierarchy that describes objects, each objects adds some properties of its own.
I store this properties in a map. Now the map is a member of the base class. Now the problem: The set of properties for each class is known and fixed (its like it were members in the descendant class) and its same for all instances of the class (and there are as many as 1000s and more). If I could have a map where the keys are static and the values are not it would exactly match my requirement, as it would function as a map, though it would cost much less memory. I hope the question is clear, anyway I'll give an example:

class A
{
protected MyMap properties;
}

class B extends class A
{
f1()
{
properties.set("1",object1);
properties.set("2",object2);
}
}

now suppose I have 1000 instances of B, then I would have 999 redundant sets of keys, as I know initially the set of keys that class B supports.

Any ideas?

   <<Less

Re: I need a tricky Map

Posted By:   Kevin_Riff  
Posted On:   Tuesday, November 6, 2001 09:00 AM

I don't think the situation is as bad as you think. The objects that form your keys can be reused in each map. So if you have 1000 maps, then one object can serve as a key in all of them at the same time.



If your keys are strings, then you can benefit from an important property of string literals. Each time the same string appears in your program, it is the same object. So as long as you use literals (not new String("key"), for example) then you needn't worry about creating an excessive number of objects.



If your keys aren't strings, then you might consider using a singleton pattern. Search the FAQ for the term "singleton". There should be tons of information there.

About | Sitemap | Contact