Java - multiple hashmaps pointing to the same key

Timegate :

I have multiple files which contains key=value string pairs. The keys are the same between the files, but the values differs. Each file can have 1000 plus of such pairs.

I want to store each file in a separate hashmap, ie map<KeyString, ValueString>, so if there are five files, then there will be five hashmaps.

To avoid duplicating the keys across each hashmap, is it possible to have each map reference the same key? Note that once the keys are added to the map, it will not be deleted.

I considered making the first file the 'base' as in the flyweight pattern, this base would be the intrinsic set of keys/values. The other remaining files would be the extrinsic set of values, but I don't know how to relate the values back to the base (intrinsic) keys without duplicating the keys?

I am open to a simpler/better approach.

kewne :

After reading in the keys, you can use String.intern(). When called, what it does is either:

  • add the String to the internal pool if it didn't exist already;
  • return the equivalent String from the pool if it already existed.

String#intern Javadoc

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=463939&siteId=1