Skip to content

Conversation

@doppleware
Copy link

This PR fixes an IllegalStateException that occurs when trying to map pets to owners when an owner has multiple pets.

Problem:
The current implementation uses Collectors.toMap() which fails with a duplicate key exception when an owner has multiple pets, as it tries to create a map with owner ID as key and a single pet ID as value.

Solution:
Changed the implementation to use Collectors.groupingBy() with Collectors.mapping() to properly handle multiple pets per owner by collecting them into lists.

Changes:

  • Modified the stream collector in getOwnerPetsMap() to properly group multiple pets per owner
  • Replaced Collectors.toMap() with Collectors.groupingBy() and Collectors.mapping()

Testing:
This change should be tested with:

  1. Owners with single pets
  2. Owners with multiple pets
  3. Owners with no pets

Fixes the IllegalStateException: "Duplicate key 3 (attempted merging values [3] and [4])"

@doppleware doppleware closed this May 31, 2025
@doppleware doppleware deleted the fix-owner-pets-duplicate-key-created-by-agentic branch May 31, 2025 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants