0

Am looking for an efficient way to initialize a HashMap given that it's keys remain the same for any object created of the class it belongs to.

My current implementation initializes the HashMap everytime a new class object is created.

Thanks!

2
  • If I understand this correctly, you have a class Foo with a field Bar that is a HashMap of some type and for all Foo, the HashMap Bar contains the same keys? Do the values change? Commented Nov 12, 2013 at 19:42
  • Yes, the values change, and you've understood right. Commented Nov 12, 2013 at 19:45

1 Answer 1

1

You can create a template Map, and use .clone() to generate new instances. You could also create a custom Map implementation that uses a copy-on-write policy to refer to a template "parent" Map.

Sign up to request clarification or add additional context in comments.

4 Comments

something along the lines of shallowCopy = (Map<String, Object>) ((HashMap<String, Object>) data).clone();, where data is a template static map?
Yep... That's one way which might work for you. TMTOWTDI. (There's More Than One Way To Do It).
it'd sure be nice to see another way...:)
Create your own HashMap extension facade that takes a "parent" Map as a constructor. In all the access methods, check to see if the key is known to the instance first. If there, return that; if not, return the parent's instance value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.