diff --git a/docs/pet-kata/slides.md b/docs/pet-kata/slides.md index 3bcad21d..a5ba8b17 100644 --- a/docs/pet-kata/slides.md +++ b/docs/pet-kata/slides.md @@ -940,12 +940,31 @@ public void getAgeStatisticsOfPets() } ``` +Bob Smith's Pet Names as String +-------------------------- +```java +@Test +public void bobSmithsPetNamesAsString() +{ + //find Bob Smith + Person person = this.people + .detect(each -> each.named("Bob Smith")); + + //get Bob Smith's pets' names + String names = person.getPets() + .collect(Pet::getName) + .makeString(" & "); + + Assertions.assertEquals("Dolly & Spot", names); +} +``` + -Stream to EC refactor #1 +Bob Smith's Pet Names as String ------------------------ ```java @Test -public void streamsToECRefactor1() +public void bobSmithsPetNamesAsString() { // Find Bob Smith Person person = this.people.detect(each -> each.named("Bob Smith")); @@ -960,33 +979,29 @@ public void streamsToECRefactor1() ``` -Stream to EC refactor #2 ------------------------- +Immutable Pet Counts by Emoji +-------------------------- ```java @Test -public void streamsToECRefactor2() +public void immutablePetCountsByEmoji() { - // Hint: Try to replace the Map with a Bag - MutableBag petTypes = this.people - .asUnmodifiable() - .flatCollect(Person::getPets) - .countBy(Pet::getType); - - Assertions.assertEquals(2, petTypes.occurrencesOf(PetType.CAT)); - Assertions.assertEquals(2, petTypes.occurrencesOf(PetType.DOG)); - Assertions.assertEquals(2, petTypes.occurrencesOf(PetType.HAMSTER)); - Assertions.assertEquals(1, petTypes.occurrencesOf(PetType.SNAKE)); - Assertions.assertEquals(1, petTypes.occurrencesOf(PetType.TURTLE)); - Assertions.assertEquals(1, petTypes.occurrencesOf(PetType.BIRD)); + // Hint: Try to replace the immutable Map with an ImmutableBag + ImmutableBag countsByEmoji = Bags.immutable.withAll( + this.people.flatCollect(Person::getPetTypes).countBy(PetType::toString) + ); + Assertions.assertEquals( + Bags.immutable.of("🐱", "🐱", "🐶", "🐶", "🐹", "🐹", "🐍", "🐢", "🐦"), + countsByEmoji + ); } ``` -Stream to EC refactor #3 +Top Three Pets ------------------------ ```java @Test -public void streamsToECRefactor3() +public void topThreePets() { // Hint: The result of groupingBy/counting can almost always be replaced by a Bag // Hint: Look for the API on Bag that might return the top 3 pet types diff --git a/pet-kata/src/test/java/org/eclipse/collections/petkata/Exercise4Test.java b/pet-kata/src/test/java/org/eclipse/collections/petkata/Exercise4Test.java index b0f0da2d..7d5fafe6 100644 --- a/pet-kata/src/test/java/org/eclipse/collections/petkata/Exercise4Test.java +++ b/pet-kata/src/test/java/org/eclipse/collections/petkata/Exercise4Test.java @@ -140,7 +140,7 @@ public void immutablePetCountsByEmoji() .collect(Collectors.groupingBy(pet -> pet.getType().toString(), Collectors.counting()))); Assertions.assertEquals( - Map.of("🐱", 2L, "🐶", 2L, "🐹", 2L, "🐍", 1L, "🐢", 1L, "🐦", 1L), + Bags.immutable.of("🐱", "🐱", "🐶", "🐶", "🐹", "🐹", "🐍", "🐢", "🐦"), countsByEmoji ); }