OnJava8-Examples/collections/MapOfList.java

62 lines
1.9 KiB
Java
Raw Normal View History

2015-12-15 11:47:04 -08:00
// collections/MapOfList.java
// (c)2016 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.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-12-15 11:47:04 -08:00
package collections;
2015-06-15 17:47:35 -07:00
import typeinfo.pets.*;
import java.util.*;
public class MapOfList {
2015-11-03 12:00:44 -08:00
public static Map<Person, List< ? extends Pet>>
2015-06-15 17:47:35 -07:00
petPeople = new HashMap<>();
static {
petPeople.put(new Person("Dawn"),
2015-09-07 11:44:36 -06: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"),
new Cat("Stanford aka Stinky el Negro"),
new Cat("Pinkola")));
2015-06-15 17:47:35 -07:00
petPeople.put(new Person("Luke"),
Arrays.asList(new Rat("Fuzzy"), new Rat("Fizzy")));
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:
2015-06-15 17:47:35 -07:00
People: [Person Marilyn, Person Dawn, Person Luke, Person
Isaac, Person Kate]
Pets: [[Pug Louie aka Louis Snorkelstein Dupree, Cat
Stanford aka Stinky el Negro, Cat Pinkola], [Cymric Molly,
Mutt Spot], [Rat Fuzzy, Rat Fizzy], [Rat Freckly], [Cat
Shackleton, Cat Elsie May, Dog Margrett]]
Person Marilyn has:
Pug Louie aka Louis Snorkelstein Dupree
Cat Stanford aka Stinky el Negro
Cat Pinkola
Person Dawn has:
Cymric Molly
Mutt Spot
Person Luke has:
Rat Fuzzy
Rat Fizzy
Person Isaac has:
Rat Freckly
Person Kate has:
Cat Shackleton
Cat Elsie May
Dog Margrett
2015-09-07 11:44:36 -06:00
*/