From 126caa6517cd68b4dc5925e39e2079e5c682e9fe Mon Sep 17 00:00:00 2001 From: Darshit Patel Date: Thu, 28 Sep 2023 23:08:15 +0530 Subject: [PATCH 1/7] Change an Immutable Map to an Immutable Bag --- .../java/org/eclipse/collections/petkata/Exercise4Test.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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..e993b41b 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 ); } From e32cb277015a5b2650bc190ae6cd7daadf24f231 Mon Sep 17 00:00:00 2001 From: Darshit Patel Date: Thu, 28 Sep 2023 23:18:56 +0530 Subject: [PATCH 2/7] Add "Bob Smith's Pet Names as String" and "Immutable Pet Counts by Emoji" Solution for --- docs/pet-kata/slides.md | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/docs/pet-kata/slides.md b/docs/pet-kata/slides.md index 3bcad21d..8c18a0e4 100644 --- a/docs/pet-kata/slides.md +++ b/docs/pet-kata/slides.md @@ -940,6 +940,49 @@ public void getAgeStatisticsOfPets() } ``` +Bob Smith's Pet Names as String +-------------------------- +```java +@Test +public void bobSmithsPetNamesAsString() +{ + // Assertions.fail("Refactor to Eclipse Collections. Don't forget to comment this out or delete it when you are done."); + + //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); +} +``` + +Immutable Pet Counts by Emoji +-------------------------- +```java +@Test +public void immutablePetCountsByEmoji() +{ + // Assertions.fail("Refactor to Eclipse Collections. Don't forget to comment this out or delete it when you are done."); + + // Hint: Try to replace the immutable Map with an ImmutableBag + // Map countsByEmoji = + // Map.copyOf(this.people + // .stream() + // .flatMap(person -> person.getPets().stream()) + // .collect(Collectors.groupingBy(pet -> pet.getType().toString(), Collectors.counting()))); + + ImmutableBag countsByEmoji = Bags.immutable.withAll(this.people.flatCollect(Person::getPetTypes).countBy(PetType::toString)); + Assertions.assertEquals( + Bags.immutable.of("🐱", "🐱", "🐶", "🐶", "🐹", "🐹", "🐍", "🐢", "🐦"), + countsByEmoji + ); +} +``` Stream to EC refactor #1 ------------------------ From c04162a892af02f9f73a3e86f8f177f87e20d24b Mon Sep 17 00:00:00 2001 From: Darshit Patel Date: Thu, 28 Sep 2023 23:59:38 +0530 Subject: [PATCH 3/7] Change generic streamsToECRefactor methods to actual method names in Solutions --- docs/pet-kata/slides.md | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/docs/pet-kata/slides.md b/docs/pet-kata/slides.md index 8c18a0e4..b7ab6985 100644 --- a/docs/pet-kata/slides.md +++ b/docs/pet-kata/slides.md @@ -967,28 +967,22 @@ Immutable Pet Counts by Emoji @Test public void immutablePetCountsByEmoji() { - // Assertions.fail("Refactor to Eclipse Collections. Don't forget to comment this out or delete it when you are done."); - // Hint: Try to replace the immutable Map with an ImmutableBag - // Map countsByEmoji = - // Map.copyOf(this.people - // .stream() - // .flatMap(person -> person.getPets().stream()) - // .collect(Collectors.groupingBy(pet -> pet.getType().toString(), Collectors.counting()))); - - ImmutableBag countsByEmoji = Bags.immutable.withAll(this.people.flatCollect(Person::getPetTypes).countBy(PetType::toString)); + ImmutableBag countsByEmoji = Bags.immutable.withAll( + this.people.flatCollect(Person::getPetTypes).countBy(PetType::toString) + ); Assertions.assertEquals( - Bags.immutable.of("🐱", "🐱", "🐶", "🐶", "🐹", "🐹", "🐍", "🐢", "🐦"), - countsByEmoji + Bags.immutable.of("🐱", "🐱", "🐶", "🐶", "🐹", "🐹", "🐍", "🐢", "🐦"), + countsByEmoji ); } ``` -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")); @@ -1003,11 +997,11 @@ 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 From b8bca79df5501c01bc0bb24d778ae6f6cc794da5 Mon Sep 17 00:00:00 2001 From: Darshit Patel Date: Fri, 29 Sep 2023 00:00:57 +0530 Subject: [PATCH 4/7] Change generic streamsToECRefactor methods to actual method names in Solutions --- docs/pet-kata/slides.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/pet-kata/slides.md b/docs/pet-kata/slides.md index b7ab6985..d5917b39 100644 --- a/docs/pet-kata/slides.md +++ b/docs/pet-kata/slides.md @@ -1019,11 +1019,11 @@ public void immutablePetCountsByEmoji() ``` -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 From 6bf0a29c1308cb6233e6aeb5a9e761a62a8860f4 Mon Sep 17 00:00:00 2001 From: Darshit Patel Date: Fri, 29 Sep 2023 00:05:47 +0530 Subject: [PATCH 5/7] Fix formatting --- .../java/org/eclipse/collections/petkata/Exercise4Test.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e993b41b..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( - Bags.immutable.of("🐱", "🐱", "🐶", "🐶", "🐹", "🐹", "🐍", "🐢", "🐦"), + Bags.immutable.of("🐱", "🐱", "🐶", "🐶", "🐹", "🐹", "🐍", "🐢", "🐦"), countsByEmoji ); } From cb8fd59e093f909da21bb70ddfc313a60ec561fc Mon Sep 17 00:00:00 2001 From: Darshit Patel Date: Fri, 29 Sep 2023 00:07:17 +0530 Subject: [PATCH 6/7] Remove commented assertion message from slides.md for newly added bobSmithsPetNamesAsString --- docs/pet-kata/slides.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/pet-kata/slides.md b/docs/pet-kata/slides.md index d5917b39..dbc9bf21 100644 --- a/docs/pet-kata/slides.md +++ b/docs/pet-kata/slides.md @@ -946,8 +946,6 @@ Bob Smith's Pet Names as String @Test public void bobSmithsPetNamesAsString() { - // Assertions.fail("Refactor to Eclipse Collections. Don't forget to comment this out or delete it when you are done."); - //find Bob Smith Person person = this.people .detect(each -> each.named("Bob Smith")); From e178acf34ff1c812fbb086c5f6a07e6a87d7f686 Mon Sep 17 00:00:00 2001 From: Darshit Patel Date: Fri, 29 Sep 2023 00:21:48 +0530 Subject: [PATCH 7/7] Fix older test solution by the correct test and solution --- docs/pet-kata/slides.md | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/docs/pet-kata/slides.md b/docs/pet-kata/slides.md index dbc9bf21..a5ba8b17 100644 --- a/docs/pet-kata/slides.md +++ b/docs/pet-kata/slides.md @@ -959,22 +959,6 @@ public void bobSmithsPetNamesAsString() } ``` -Immutable Pet Counts by Emoji --------------------------- -```java -@Test -public void immutablePetCountsByEmoji() -{ - // 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 - ); -} -``` Bob Smith's Pet Names as String ------------------------ @@ -996,23 +980,19 @@ public void bobSmithsPetNamesAsString() Immutable Pet Counts by Emoji ------------------------- +-------------------------- ```java @Test 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 + ); } ```