55 lines
1.9 KiB
Java
55 lines
1.9 KiB
Java
import java.util.*;
|
|
|
|
public class Debugging {
|
|
public static void main(String[] args) {
|
|
Map<String, Set<Integer>> testMap = new TreeMap<>();
|
|
Set<Integer> c121 = arrToSet(new int[]{42, 17, 42, 42});
|
|
Set<Integer> c122 = arrToSet(new int[]{10, 12, 14});
|
|
Set<Integer> c123 = arrToSet(new int[]{100, 99, 98, -97});
|
|
testMap.put("cse121", c121);
|
|
testMap.put("cse122", c122);
|
|
testMap.put("cse123", c123);
|
|
|
|
Map<String, Set<Integer>> deepCopyMap = deepCopy(testMap);
|
|
|
|
if (deepCopyMap.isEmpty()) {
|
|
System.out.println("{}");
|
|
} else {
|
|
String line = "";
|
|
for (String key : deepCopyMap.keySet()) {
|
|
line += key + "=" + deepCopyMap.get(key).toString() + ", ";
|
|
}
|
|
System.out.println("{" + line.substring(0, line.length() - 2) + "}");
|
|
}
|
|
}
|
|
|
|
public static Set<Integer> arrToSet(int[] arr) {
|
|
Set<Integer> s = new TreeSet<>();
|
|
for (int num : arr) {
|
|
s.add(num);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
// Produces and returns a "deep copy" of the parameter map, which has the same
|
|
// structure and values as the parameter, but with all internal data structures
|
|
// and values copied. After calling this method, modifying the parameter or
|
|
// return value should NOT affect the other.
|
|
//
|
|
// Parameters:
|
|
// inputMap - the map to duplicate
|
|
//
|
|
// Returns:
|
|
// A deep copy of the parameter map.
|
|
public static Map<String, Set<Integer>> deepCopy(Map<String, Set<Integer>> inputMap) {
|
|
Map<String, Set<Integer>> deepCopy = new TreeMap<>();
|
|
|
|
for (String key : inputMap.keySet()) {
|
|
Set<Integer> inputSet = new TreeSet<>(inputMap.get(key));
|
|
// Set<Integer> inputSet = inputMap.get(key);
|
|
deepCopy.put(key, inputSet);
|
|
}
|
|
return deepCopy;
|
|
}
|
|
}
|