← Java from Scratch Lesson 78 of 88 · 10 XP

TreeMap

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

TreeMap is a dictionary with its keys in order (a tree, O(log n)) and a search for the nearest keys:

TreeMap<Integer, String> grades = new TreeMap<>();
grades.put(90, "A"); grades.put(75, "B"); grades.put(60, "C"); grades.put(0, "F");
grades.floorEntry(82)     // 75=B — the largest key ≤ 82
grades.ceilingKey(61)     // 75
grades.firstKey(), grades.lastEntry()
grades.headMap(75)        // keys < 75
grades.descendingMap()    // in reverse order

floorEntry turns a "staircase of thresholds" into one line: a grade by score, a tariff by volume, a level by experience.

Task. The first line is the level thresholds xp:name separated by spaces (for example 0:Novice 100:Apprentice 500:Master). After that come players, nickname xp. For each one print nickname — level, and at the end print how many players are at each level, in the order of the thresholds (name: N, including levels with no players).

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.