multiple hashmaps pointing to the same key

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.

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

    I can think about a simpler approach. Instead of having Map<String, String> think of Map<String, List<String> or directly MultiMap<String, String> from guava .

    If each key is in each file and all have values, you could store values from first file at 0th index, from the second at 1st index etc.

    If it wouldn't work, I recommend a Collection<Map<String, String> , so you're able to iterate through your Map s. Then when you want to add value to one of the Map s, go through all keySet s and if one of them contains that key, just put with object returned from this keySet .

    Other solution would be to have a HashSet of keys that have already been put. This would be more efficient.

    First of all, I don't see the problem with storing multiple instances of your String keys. 5 HashMap s * 1000 keys is a very small number, and you shouldn't have memory issues.

    That said, if you still want to avoid duplicating the String s, you can create the first HashMap , and then you the exact same keys for the other HashMap s.

    For example, suppose map1 is the first HashMap and it is already populated with the contents of the first file.

    You can write something like this to populate the 2nd HashMap :

    for (String key : map1.keySet()) {
        map2.put (key, someValue);

    Of course you will have to find for each key of the first map the corresponding value of the second map. If the keys are not stored in the same order in the input files, this may require some preliminary sorting step.


    上一篇: 生产中的Windows上的Docker

    下一篇: 多个hashmaps指向相同的密钥