0

I would like to get to know which method is actually better to use when I need to create array containing many maps with exact same keys in it. I just think about 3 methods here:

  1. Reinitialize the new HashMap().
    Map t = new HashMap();
    t.put(k, v);
    list.add(t);
    t = new HashMap();
    t.put(k, v);
    list.add(t);
    // ... and so on
    
  2. Use map.clear().
    Map t = new HashMap();
    t.put(k, v);
    list.add(t);
    t.clear();
    t.put(k, v);
    list.add(t);
    // ... and so on
    
  3. Just put() right away.
    Map t = new HashMap();
    t.put(k, v);
    list.add(t);
    t.put(k, v);
    list.add(t);
    // ... and so on
    

I have read the articles about the use of new HashMap() vs map.clear() here. However, for performance-wise, I still look for "more-optimized" one. Thus, I decided to take a look of put method of Java Map.

When I take a look at Java doc of Map, I saw that the put() function is actually check if key exists before place the value. What it actually does is like below:

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

Suppose that the data is so so many (just say 10000 maps to be inserted to the list and each map has 100 key-value pairs) What is actually the best practice for the performance:

  1. Time-wise (Runtime)
  2. Resource-wise (Memory)

among these three methods while considering the data integrity (expected result is no any mistake for each maps inside the list)?

Dhana D.
  • 1,670
  • 3
  • 9
  • 33
  • 1
    Only your first option uses multiple maps. – Kayaman Feb 22 '22 at 11:19
  • Does this answer your question? [Map.clear() vs new Map : Which one will be better?](https://stackoverflow.com/questions/6757868/map-clear-vs-new-map-which-one-will-be-better) – Saif Ahmad Feb 22 '22 at 11:21
  • @SaifAhmad That's the article I mentioned in my question :) . However it doesn't answer about the comparison of them vs using put right away – Dhana D. Feb 22 '22 at 11:23

1 Answers1

1

Only your first version will work at all!! The two others ones will add always the same map to the list!!! So the questions regarding performance or resource utilisation are futile.

As all instance have the same contents, equals() on the list entries will always return true, but for your versions 2 and 3, also == on them would return true.

If this is intended, why populating a list with the map?

List.add() will not store a copy of the instance referenced by the argument (at least not in Java), it will just keep the reference.

tquadrat
  • 3,033
  • 1
  • 16
  • 29