0Pricing
Java Academy · Lesson

Maps & Frequency Counting

Map basics (put/get/remove), entry iteration, word-frequency counter, and TreeMap for sorted view.

Maps & Frequency Counting is a free Java Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is a Map?

A Map stores key→value pairs. Good choices:

  • HashMap: fast, no order.
  • TreeMap: keys sorted.
  • LinkedHashMap: remembers insertion order.

Common operations

Main operations:

  • put(k,v), get(k), containsKey(k), remove(k)
  • Size: size()
  • Loop: entrySet() for key and value.

Code: Map basics

Keys are unique. Reusing a key replaces its value. Use entrySet() to loop over entries.

public class Main {
  public static void main(String[] args) {
    java.util.Map<String, Integer> scores = new java.util.HashMap<>();
    scores.put("Ada", 90);
    scores.put("Bob", 75);
    scores.put("Ada", 95); // replaces old value

    System.out.println("Bob's score=" + scores.get("Bob"));
    System.out.println("Has Ada? " + scores.containsKey("Ada"));
    scores.remove("Bob");

    for (java.util.Map.Entry<String, Integer> e : scores.entrySet()) {
      System.out.println(e.getKey() + " -> " + e.getValue());
    }
  }
}

Frequency idea

Word count: Loop through words and update with getOrDefault: map.put(w, map.getOrDefault(w,0)+1).

Code: word frequency

getOrDefault makes counting short. Each word adds +1 to its current count.

public class Main {
  public static void main(String[] args) {
    String[] words = { "to", "be", "or", "not", "to", "be" };
    java.util.Map<String,Integer> counter = new java.util.HashMap<>();

    for (String w : words) {
      int newVal = counter.getOrDefault(w, 0) + 1;
      counter.put(w, newVal);
    }
    System.out.println(counter);
  }
}

Code: TreeMap sorted

TreeMap sorts keys automatically. A quick way to print results in alphabetical order.

public class Main {
  public static void main(String[] args) {
    java.util.Map<String,Integer> counts = new java.util.HashMap<>();
    counts.put("banana",2);
    counts.put("apple",3);
    counts.put("cherry",1);

    java.util.Map<String,Integer> sorted = new java.util.TreeMap<>(counts);
    for (java.util.Map.Entry<String,Integer> e : sorted.entrySet()) {
      System.out.println(e.getKey() + " -> " + e.getValue());
    }
  }
}

HashMap check

Quick check: Which statement about HashMap is correct?

Recap

Recap: Maps store key→value pairs. You learned put/get/remove, word counting with getOrDefault, and sorted views with TreeMap.

Frequently asked questions

Is the “Maps & Frequency Counting” lesson free?

Yes — the full text of “Maps & Frequency Counting” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Maps & Frequency Counting”?

Map basics (put/get/remove), entry iteration, word-frequency counter, and TreeMap for sorted view. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Maps & Frequency Counting” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Maps & Frequency Counting
  2. Comparable vs Comparator
  3. Sorting/Searching with Collections
← Back to Java Academy