2015-12-15 11:47:04 -08:00
|
|
|
// collections/MapOfList.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-07-28 12:48:23 -06:00
|
|
|
// {java collections.MapOfList}
|
2015-12-15 11:47:04 -08:00
|
|
|
package collections;
|
2021-03-04 16:15:04 -07:00
|
|
|
import reflection.pets.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class MapOfList {
|
2017-05-01 17:43:21 -06:00
|
|
|
public static final Map<Person, List< ? extends Pet>>
|
2015-06-15 17:47:35 -07:00
|
|
|
petPeople = new HashMap<>();
|
|
|
|
static {
|
|
|
|
petPeople.put(new Person("Dawn"),
|
2016-01-25 18:05:55 -08:00
|
|
|
Arrays.asList(
|
|
|
|
new Cymric("Molly"),
|
|
|
|
new Mutt("Spot")));
|
2015-06-15 17:47:35 -07:00
|
|
|
petPeople.put(new Person("Kate"),
|
|
|
|
Arrays.asList(new Cat("Shackleton"),
|
|
|
|
new Cat("Elsie May"), new Dog("Margrett")));
|
|
|
|
petPeople.put(new Person("Marilyn"),
|
|
|
|
Arrays.asList(
|
2015-09-07 11:44:36 -06:00
|
|
|
new Pug("Louie aka Louis Snorkelstein Dupree"),
|
2017-05-04 10:09:51 -06:00
|
|
|
new Cat("Stanford"),
|
2015-09-07 11:44:36 -06:00
|
|
|
new Cat("Pinkola")));
|
2015-06-15 17:47:35 -07:00
|
|
|
petPeople.put(new Person("Luke"),
|
2017-05-10 11:45:39 -06:00
|
|
|
Arrays.asList(
|
|
|
|
new Rat("Fuzzy"), new Rat("Fizzy")));
|
2015-06-15 17:47:35 -07:00
|
|
|
petPeople.put(new Person("Isaac"),
|
|
|
|
Arrays.asList(new Rat("Freckly")));
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("People: " + petPeople.keySet());
|
|
|
|
System.out.println("Pets: " + petPeople.values());
|
2015-06-15 17:47:35 -07:00
|
|
|
for(Person person : petPeople.keySet()) {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(person + " has:");
|
2015-06-15 17:47:35 -07:00
|
|
|
for(Pet pet : petPeople.get(person))
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(" " + pet);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2017-05-03 12:00:00 -06:00
|
|
|
People: [Person Dawn, Person Kate, Person Isaac, Person
|
|
|
|
Marilyn, Person Luke]
|
|
|
|
Pets: [[Cymric Molly, Mutt Spot], [Cat Shackleton, Cat
|
|
|
|
Elsie May, Dog Margrett], [Rat Freckly], [Pug Louie aka
|
2017-05-04 10:09:51 -06:00
|
|
|
Louis Snorkelstein Dupree, Cat Stanford, Cat Pinkola],
|
|
|
|
[Rat Fuzzy, Rat Fizzy]]
|
2015-06-15 17:47:35 -07:00
|
|
|
Person Dawn has:
|
|
|
|
Cymric Molly
|
|
|
|
Mutt Spot
|
|
|
|
Person Kate has:
|
|
|
|
Cat Shackleton
|
|
|
|
Cat Elsie May
|
|
|
|
Dog Margrett
|
2017-05-03 12:00:00 -06:00
|
|
|
Person Isaac has:
|
|
|
|
Rat Freckly
|
|
|
|
Person Marilyn has:
|
|
|
|
Pug Louie aka Louis Snorkelstein Dupree
|
2017-05-04 10:09:51 -06:00
|
|
|
Cat Stanford
|
2017-05-03 12:00:00 -06:00
|
|
|
Cat Pinkola
|
|
|
|
Person Luke has:
|
|
|
|
Rat Fuzzy
|
|
|
|
Rat Fizzy
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|