// Exercise 2: Map operations — computeIfAbsent, merge, getOrDefault
//
// Implement the three methods below using java.util.Map / HashMap.

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class java_collections_framework_exercise_2 {

    /**
     * Count occurrences of each word in `words`, returning a Map from
     * word -> count. Use computeIfAbsent or merge — not manual
     * containsKey/get/put chains.
     */
    static Map<String, Integer> wordCounts(List<String> words) {
        // TODO: implement
        return null;
    }

    /**
     * Group the given integers by whether they are even or odd, returning
     * a Map with keys "even" and "odd" mapping to Lists of the numbers
     * that belong in each group, preserving input order within each group.
     */
    static Map<String, List<Integer>> groupByParity(List<Integer> numbers) {
        // TODO: implement using computeIfAbsent(key, k -> new ArrayList<>())
        return null;
    }

    /**
     * Return the value for `key` in `map`, or `defaultValue` if absent —
     * without throwing and without a manual containsKey check.
     */
    static int lookupOrDefault(Map<String, Integer> map, String key, int defaultValue) {
        // TODO: implement using getOrDefault
        return 0;
    }

    // --- self-check: do not modify below this line ---
    public static void main(String[] args) {
        boolean ok = true;

        Map<String, Integer> counts = wordCounts(List.of("a", "b", "a", "c", "b", "a"));
        ok &= counts.get("a") == 3;
        ok &= counts.get("b") == 2;
        ok &= counts.get("c") == 1;

        Map<String, List<Integer>> grouped = groupByParity(List.of(1, 2, 3, 4, 5, 6));
        ok &= grouped.get("even").equals(List.of(2, 4, 6));
        ok &= grouped.get("odd").equals(List.of(1, 3, 5));

        Map<String, Integer> m = new HashMap<>(Map.of("x", 1));
        ok &= lookupOrDefault(m, "x", -1) == 1;
        ok &= lookupOrDefault(m, "missing", -1) == -1;

        System.out.println(ok ? "PASS" : "FAIL");
    }
}
