summaryrefslogtreecommitdiff
path: root/debugging/Debugging.java
blob: a9e301f632467583eb3f2b5b55a88ddf2eb962ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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;
    }
}