I am having difficulty making a deep copy of a HashMap. I tried the code below from How to copy HashMap (not shallow copy) in Java, 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.
private HashMap<Character, ArrayList<int[]>> board;
public Vertex(HashMap<Character, ArrayList<int[]>> inputboard) {
board = new HashMap<>();
this.board = copy(inputboard);
}
public HashMap<Character, ArrayList<int[]>> copy (HashMap<Character, ArrayList<int[]>> original) {
HashMap<Character, ArrayList<int[]>> copy = original.entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> List.copyOf(e.getValue())));
return copy;
}
Before I used the above code, I also tried this from How to copy HashMap (not shallow copy) in Java but it did not make a deep copy
private HashMap<Character, ArrayList<int[]>> board;
public Vertex(HashMap<Character, ArrayList<int[]>> inputboard) {
board = new HashMap<>();
this.board = copy(inputboard);
}
public HashMap<Character, ArrayList<int[]>> copy (HashMap<Character, ArrayList<int[]>> original) {
HashMap<Character, ArrayList<int[]>> copy = new HashMap<>();
for (Map.Entry<Character, ArrayList<int[]>> entry : original.entrySet()) {
copy.put(entry.getKey(), new ArrayList<>(entry.getValue()));
}
return copy;
}