| 8 | import java.util.*; |
| 9 | |
| 10 | public class MapOfList { |
| 11 | public static final Map<Person, List< ? extends Pet>> |
| 12 | petPeople = new HashMap<>(); |
| 13 | static { |
| 14 | petPeople.put(new Person("Dawn"), |
| 15 | Arrays.asList( |
| 16 | new Cymric("Molly"), |
| 17 | new Mutt("Spot"))); |
| 18 | petPeople.put(new Person("Kate"), |
| 19 | Arrays.asList(new Cat("Shackleton"), |
| 20 | new Cat("Elsie May"), new Dog("Margrett"))); |
| 21 | petPeople.put(new Person("Marilyn"), |
| 22 | Arrays.asList( |
| 23 | new Pug("Louie aka Louis Snorkelstein Dupree"), |
| 24 | new Cat("Stanford"), |
| 25 | new Cat("Pinkola"))); |
| 26 | petPeople.put(new Person("Luke"), |
| 27 | Arrays.asList( |
| 28 | new Rat("Fuzzy"), new Rat("Fizzy"))); |
| 29 | petPeople.put(new Person("Isaac"), |
| 30 | Arrays.asList(new Rat("Freckly"))); |
| 31 | } |
| 32 | public static void main(String[] args) { |
| 33 | System.out.println("People: " + petPeople.keySet()); |
| 34 | System.out.println("Pets: " + petPeople.values()); |
| 35 | for(Person person : petPeople.keySet()) { |
| 36 | System.out.println(person + " has:"); |
| 37 | for(Pet pet : petPeople.get(person)) |
| 38 | System.out.println(" " + pet); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | /* Output: |
| 43 | People: [Person Dawn, Person Kate, Person Isaac, Person |
| 44 | Marilyn, Person Luke] |