Java Arena
Step 7 of 9

Java's Map interface

Maps work the same way. HashMap is a class, and it implements an interface called Map, also in java.util. Map promises what modules 31 to 33 used: put, get, getOrDefault, containsKey, remove, size, keySet and values. A bakery keeps the day's orders, from each kind of bread to a number of loaves:

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

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> orders = new HashMap<>();
        orders.put("rye", 4);
        orders.put("sourdough", 10);
        orders.put("rye", orders.get("rye") + 2);
        orders.put("baguette", 7);
        System.out.println(orders.get("rye"));
        System.out.println(orders.getOrDefault("spelt", 0));
        System.out.println(totalLoaves(orders));
        printOrders(orders);
    }

    public static int totalLoaves(Map<String, Integer> orders) {
        int total = 0;
        for (int loaves : orders.values()) {
            total += loaves;
        }
        return total;
    }

    public static void printOrders(Map<String, Integer> orders) {
        List<String> breads = new ArrayList<>(orders.keySet());
        Collections.sort(breads);
        for (String bread : breads) {
            System.out.println(bread + ": " + orders.get(bread));
        }
    }
}
Output
6
0
23
baguette: 7
rye: 6
sourdough: 10
  • Map<String, Integer> orders = new HashMap<>();: the variable's type is the interface, and the object is a HashMap. Map needs its own import, import java.util.Map;.
  • Every call you know from HashMap works, since every one of them is in Map.
  • totalLoaves and printOrders take a Map, so they'd work with any class that implements Map. Java has others, such as TreeMap, which keeps its keys in sorted order. This course keeps to HashMap.
  • The object is still a HashMap, so module 31's rule about order still holds: it goes through its keys in an order set by their hash codes. That's why printOrders sorts a list of the keys first. The list is a List<String> now, like step 6's lists.

What keySet and values give

keySet() and values() return interface types too:

  • keySet() returns a Set<String>, a set of the keys. Sets are the next step.
  • values() returns a Collection<Integer>. Collection is the interface of step 9.

So far you've used them only in a for-each loop or in new ArrayList<>(...), and both of those accept any Set or Collection.

Module 33's maps of lists can use the interfaces too: Map<String, List<String>> byClub = new HashMap<>(); holds a List for each key, and each new value can still be made with new ArrayList<>(), since an ArrayList is a List.

Your task

Challenge 1 of 3

The baker starts with the bread that has the most loaves ordered. Write the method biggestOrder(Map<String, Integer> orders), which returns the kind of bread with the most loaves. The map always has at least one order, and no two kinds have the same number of loaves. The check runs:

Map<String, Integer> orders = new HashMap<>();
orders.put("rye", 6);
orders.put("sourdough", 10);
orders.put("baguette", 7);
System.out.println(biggestOrder(orders));

and expects:

sourdough

Challenge 2

What does this program print?

Challenge 3

The bakery lets its customers vote for next month's special bread. Write the method countVotes(List<String> votes), which returns a Map<String, Integer> from each kind of bread to the number of votes it got. The check runs:

List<String> votes = new ArrayList<>();
votes.add("walnut");
votes.add("olive");
votes.add("walnut");
Map<String, Integer> counts = countVotes(votes);
System.out.println(counts.get("walnut") + " " + counts.get("olive"));
System.out.println(counts.size());

and expects:

2 1
2