Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - multiple hashmaps pointing to the same key

Tags:

java

hashmap

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.

like image 765
Timegate Avatar asked Aug 10 '17 08:08

Timegate


People also ask

Can a HashMap have multiple values for same key Java?

HashMap can be used to store key-value pairs. But sometimes you may want to store multiple values for the same key. For example: For Key A, you want to store - Apple, Aeroplane.

How HashMap add multiple values to same key?

If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values. Show activity on this post. I use Map<KeyType, Object[]> for associating multiple values with a key in a Map. This way, I can store multiple values of different types associated with a key.

Can map have multiple values for same key?

The Map interface stores the elements as key-value pairs. It does not allow duplicate keys but allows duplicate values. HashMap and LinkedHashMap classes are the widely used implementations of the Map interface. But the limitation of the Map interface is that multiple values cannot be stored against a single key.

What if HashMap has same key?

Duplicates: HashSet doesn't allow duplicate values. HashMap stores key, value pairs and it does not allow duplicate keys. If the key is duplicate then the old key is replaced with the new value.


2 Answers

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 Maps. Then when you want to add value to one of the Maps, go through all keySets 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.

like image 119
xenteros Avatar answered Sep 28 '22 22:09

xenteros


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

like image 30
kewne Avatar answered Sep 28 '22 21:09

kewne