summaryrefslogtreecommitdiff
path: root/debugging/Debugging.java
diff options
context:
space:
mode:
Diffstat (limited to 'debugging/Debugging.java')
-rw-r--r--debugging/Debugging.java54
1 files changed, 54 insertions, 0 deletions
diff --git a/debugging/Debugging.java b/debugging/Debugging.java
new file mode 100644
index 0000000..a9e301f
--- /dev/null
+++ b/debugging/Debugging.java
@@ -0,0 +1,54 @@
+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;
+ }
+}