GROUNDWORK Chapter 9 - Maps and Sets Name: ________________________________ Date: ______________ Fill in every blank. Where a term is asked for, write the word the course uses. Where code is asked for, write exact Java. Where output is asked for, write what the program prints, one line per line. VOCABULARY ---------- 1. A __________________ stores a connection between a key and a value directly, so a lookup doesn't depend on two arrays staying in the same order. 2. In scores.put("Ada", 88), "Ada" is the __________________, which you look things up by, and 88 is the __________________ it leads to. 3. A __________________ is a collection with no duplicates, best suited to answering one question: is this thing in here? 4. A __________________ is a small method with no name, such as e -> e.getValue(), often passed to Comparator.comparing to sort a list. READING CODE ------------ 5. What does this print? Map scores = new LinkedHashMap<>(); scores.put("Ada", 88); scores.put("Grace", 92); scores.put("Alan", 45); scores.put("Ada", 91); IO.println(scores); ____________________________________ 6. What does this print? Map scores = new LinkedHashMap<>(); scores.put("Ada", 88); IO.println(scores.get("Grace")); IO.println(scores.getOrDefault("Grace", 0)); ____________________________________ 7. What does this print? String text = "a b a c b a"; Map counts = new LinkedHashMap<>(); for (String word : text.split(" ")) { counts.put(word, counts.getOrDefault(word, 0) + 1); } IO.println(counts); ____________________________________ 8. What does this print? Map scores = new LinkedHashMap<>(); scores.put("Ada", 88); scores.put("Grace", 92); scores.put("Alan", 45); String top = null; for (String name : scores.keySet()) { if (top == null || scores.get(name) > scores.get(top)) { top = name; } } IO.println(top); ____________________________________ 9. What does this print? Set band = Set.of("Ada", "Grace", "Alan"); Set chorus = Set.of("Grace", "Alan", "Katherine"); Set both = new HashSet<>(band); both.retainAll(chorus); IO.println(both.size()); Set bandOnly = new HashSet<>(band); bandOnly.removeAll(chorus); IO.println(bandOnly.contains("Ada")); ____________________________________ 10. What does this print? Map ada = new LinkedHashMap<>(); ada.put("score", "88"); ada.put("grade", "B"); Map grace = new LinkedHashMap<>(); grace.put("score", "92"); grace.put("grade", "A"); Map> students = new LinkedHashMap<>(); students.put("Ada", ada); students.put("Grace", grace); for (String name : students.keySet()) { Map info = students.get(name); String score = info.get("score"); String grade = info.get("grade"); IO.println(name + ": " + score + " (" + grade + ")"); } ____________________________________ WRITING CODE ------------ 11. Complete the line so that it prints 0 instead of null for a key the map does not have: Map scores = new LinkedHashMap<>(); scores.put("Ada", 88); IO.println(scores.__________________("Grace", 0)); // 0 12. Complete the line so that it takes the "Alan" entry out of the map entirely: // scores is {Ada=88, Grace=92, Alan=45} scores.__________________("Alan"); IO.println(scores); // {Ada=88, Grace=92} 13. Complete the line so that it removes the duplicates and keeps the numbers sorted: ArrayList numbers = new ArrayList<>(List.of(9, 30, 9, 20)); Set unique = new __________________<>(numbers); IO.println(unique); // [9, 20, 30] WHY IT WORKS ------------ 14. counts.getOrDefault(word, 0) gives back the current count, or __________________ the very first time a word is seen, so counts.put(word, counts.getOrDefault(word, 0) + 1) works whether or not the key already __________________. 15. A loop over scores.keySet() hands you the __________________ only; scores.__________________() hands you each key and its value together, as an entry.