Java - multiple hashmaps pointing to the same key

I have several files containing key = value pairs. The keys are the same between files, but the values ​​are different. Each file can have 1000 pluses of such pairs.

I want to store each file in a separate hash file, i.e. map<KeyString, ValueString> , so if there are five files, then there will be five hash maps.

To avoid duplicate keys in each hash map, is it possible for each map to refer to the same key? Please note that after adding keys to the card, it will not be deleted.

I decided to make the first file a “base”, as in the fly agaric, this base will be an internal set of keys / values. The other remaining files will be an external set of values, but I don’t know how to associate values ​​with basic (internal) keys without duplicating keys?

I am open to a simpler / better approach.

+7
java hashmap
source share
4 answers

After reading the keys, you can use String.intern() . When called, he does the following:

  • add String to the internal pool if it does not already exist;
  • returns the equivalent string from the pool if it already exists.

String # intern Javadoc

+1
source share

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

If each key is in each file and everyone has values, you can store values ​​from the first file with the 0th index, from the second to the 1st index, etc.

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

Another solution would be to have HashSet keys that have already been supplied. That would be more efficient.

+2
source share

First of all, I do not see a problem with storing multiple instances of your string keys. 5 HashMap * 1000 keys is a very small number and you should not have memory problems.

However, if you still want to avoid duplicating String s, you can create the first HashMap , and then you have exactly the same keys for 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 a second HashMap :

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

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

+1
source share

Perhaps you could hold a static Map<> to map your keys to unique Integers and use these Integer to keys to your map?

Something like:

 class KeySharedMap<K,V> { // The next key to use. Using Atomics for the auto-increment. static final AtomicInteger next = new AtomicInteger(0); // Static mapping of keys to unique Integers. static final ConcurrentMap<Object,Integer> keys = new ConcurrentHashMap<>(); // The map indexed by Integer from the `keys`. Map<Integer, V> map = new HashMap<>(); public V get(Object key) { return map.get(keys.get(key)); } public V put(Object key, V value) { // Associate a unique integer for each unique key. keys.computeIfAbsent(key,x -> next.getAndIncrement()); // Put it in my map. return map.put(keys.get(key),value); } } 

Yes, I understand that K is not used here, but I suspect that it would be necessary if you want to implement Map<K,V> .

0
source share

All Articles