Skip to content

Checkpoint: Topic Report

The Course
•
20 min study

Choose structures by the question being answered. An event list preserves occurrences, a set identifies unique values, and a map associates each value with an aggregate. The checkpoint combines them without mutating the source. Use loops first so every change to the result is visible.

Work through the example

Map entries expose one key/value pair at a time. entrySet() gives entries for traversal. Map.Entry<String, Integer> names the pair's type; getKey and getValue read it. With LinkedHashMap, this traversal follows insertion order. The loop does not need indexes or another lookup for each value.

Before Run, write the output and identify the exact line responsible for each result. Each editor is a separate program; names and edits in one box do not carry into the next.

Predict, then run Java
import java.util.Map;
import java.util.LinkedHashMap;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> totals = new LinkedHashMap<>();
        totals.put("Java", 35);
        totals.put("Tests", 10);
        for (Map.Entry<String, Integer> entry : totals.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}
Check your prediction
Text
Java: 35
Tests: 10

Compare the behavior

There can be no qualifying entries without an error. Treat empty output structures as normal results, not as failures. This loop adds only positive durations using the same index in two arrays of equal length. It assumes nonblank topics. The independent exercise below adds a separate blank-label filtering rule. A real application would keep related fields in objects; parallel arrays here are only a small trace exercise.

Trace the changed case Java
import java.util.Map;
import java.util.LinkedHashMap;

public class Main {
    public static void main(String[] args) {
        String[] topics = {"Java", "Tests"};
        int[] minutes = {0, -5};
        Map<String, Integer> totals = new LinkedHashMap<>();
        for (int i = 0; i < topics.length; i++) {
            if (minutes[i] > 0) {
                totals.put(topics[i], totals.getOrDefault(topics[i], 0) + minutes[i]);
            }
        }
        System.out.println(totals);
    }
}
Check your prediction
Text
{}

Plan your acceptance cases

Before writing the complete implementation, predict these cases without running the solution. Keep the requirements above as the authority; this table highlights distinct promises rather than prescribing a particular class layout.

CaseExpected behaviorSkill it checks
Java, Tests, JavaTwo keys; Java count twoMap aggregation
Padded Java plus JavaOne normalized keyNormalization before identity
Empty input or all blanksEmpty result, source preservedFiltering and ownership

After your first version works, choose one row and introduce a plausible defect that it should expose. Restore the correct version, then recreate the central operation without the worked example. If one skill is missing, revisit that lesson before combining more features.

Practice independently

Exercise

Checkpoint: Topic Report: practice

Implement frequencies(List<String>) for nonnull strings. Trim labels, ignore blank labels, and preserve first-seen order. For " Java ", "Tests", "Java", " ", print {Java=2, Tests=1}. Show source size remains 4 and empty input gives {}. Use one map, not a separate counter variable for every possible topic.

Spend a few minutes on your own first. Compile after one small change, read the first error, and check the result against the requirements rather than matching the solution's layout.

Your workspace Java
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.LinkedHashMap;

public class Main {
    public static void main(String[] args) {
        System.out.println("Build and test the topic report");
    }
}
Hint 1: the idea

Normalize before using a label as a key. Never change the input collection to produce a report.

Hint 2: a first step

In the loop, trim into key, skip empty text, then update the existing count or start from zero.

Show one solution

One possible implementation follows. Compare its decisions with yours: which values can change, what inputs are accepted, and what would expose a mistake?

Java
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.LinkedHashMap;

public class Main {
    public static void main(String[] args) {
        List<String> source = new ArrayList<>();
        source.add(" Java ");
        source.add("Tests");
        source.add("Java");
        source.add(" ");
        System.out.println(frequencies(source));
        System.out.println(source.size());
        System.out.println(frequencies(new ArrayList<String>()));
    }

    static Map<String, Integer> frequencies(List<String> labels) {
        Map<String, Integer> result = new LinkedHashMap<>();
        for (String label : labels) {
            String key = label.trim();
            if (!key.isEmpty()) {
                result.put(key, result.getOrDefault(key, 0) + 1);
            }
        }
        return result;
    }
}

Expected output:

Text
{Java=2, Tests=1}
4
{}

Change it and test again

Make the implementation put 1 for every occurrence. Add a check that fails. Next remove trimming: which test now distinguishes the two spellings? Restore both behaviors. Finally print the report one entry per line using entrySet().

Explain before moving on

Justify List for the source and Map for the result. Would a Set alone preserve the counts? Explain which ordering you promise and how your selected implementation supports it.

Was this article helpful?

Your response is saved on this device.