In the below key/value list “val3” is repeating for different keys. Which map suits best for such type of list.
I want “val3” should be stored only once with multiple keys pointing to it.
key1—->val1
key2—->val3
key3—->val3
key5—->val5
key6—->val3
key7—->val6
key8—->val3
key9—->val3
Any
Mapimplementation will do. Every Java collection only holds references, not actual objects, so if you put the same object several times in a map, all the values will point to the same object. In other words changes made toval3throughkey2lookup will be reflected when looking up by any other key pointing toval3.Consider simplified example:
Single
VeryLargeinstance is referenced by all1,2and3keys.