title: Deep Copy of HashMap in Java
body: <p>I am having difficulty making a deep copy of a HashMap.
  I tried the code below from <a href="https://stackoverflow.com/questions/28288546/how-to-copy-hashmap-not-shallow-copy-in-java/28288729">How to copy HashMap (not shallow copy) in Java</a>, but List at List.copyOf is giving me an error (Cannot resolve symbol 'List') and I'm not sure what to replace it with. If there is any other way to make a deep copy of HashMap, I would love to hear.</p>
  <pre><code>private HashMap&lt;Character, ArrayList&lt;int[]&gt;&gt; board;
  
  public Vertex(HashMap&lt;Character, ArrayList&lt;int[]&gt;&gt; inputboard) {
          board = new HashMap&lt;&gt;();
          this.board = copy(inputboard);
  }
  
  public HashMap&lt;Character, ArrayList&lt;int[]&gt;&gt; copy (HashMap&lt;Character, ArrayList&lt;int[]&gt;&gt; original) {  
          HashMap&lt;Character, ArrayList&lt;int[]&gt;&gt; copy = original.entrySet().stream()
                  .collect(Collectors.toMap(e -&gt; e.getKey(), e -&gt; List.copyOf(e.getValue())));
          return copy;
  }
  </code></pre>
  <p>Before I used the above code, I also tried this from <a href="https://stackoverflow.com/questions/28288546/how-to-copy-hashmap-not-shallow-copy-in-java/28288729">How to copy HashMap (not shallow copy) in Java</a> but it did not make a deep copy</p>
  <pre><code>private HashMap&lt;Character, ArrayList&lt;int[]&gt;&gt; board;
  
  public Vertex(HashMap&lt;Character, ArrayList&lt;int[]&gt;&gt; inputboard) {
          board = new HashMap&lt;&gt;();
          this.board = copy(inputboard);
  }
  
  public HashMap&lt;Character, ArrayList&lt;int[]&gt;&gt; copy (HashMap&lt;Character, ArrayList&lt;int[]&gt;&gt; original) {
         HashMap&lt;Character, ArrayList&lt;int[]&gt;&gt; copy = new HashMap&lt;&gt;();
         for (Map.Entry&lt;Character, ArrayList&lt;int[]&gt;&gt; entry : original.entrySet()) {
              copy.put(entry.getKey(), new ArrayList&lt;&gt;(entry.getValue()));
         }
         return copy;
  }
  </code></pre>