Java Tutorial
Java Project: Contact Book
Store contacts in a TreeMap from name to number, then add, find, and list them in order.
What you will build
A TreeMap keeps names sorted. put adds, get finds, forEach lists.
TreeMap of names
Example
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Map book = new TreeMap<>();
book.put("Ada", "555-0100");
book.put("Kai", "555-0142");
book.put("Rin", "555-0199");
System.out.println("Kai -> " + book.get("Kai"));
book.forEach((name, n) -> System.out.println(name + " " + n));
}
} Practice
- Compile the complete program at
/java/try. - Change one input value and compile again.
- Replace a hardcoded number with
Scannerwhen you want typed input.