From fce2d31d57314c9b3be6ab97bda4a41ba1453c13 Mon Sep 17 00:00:00 2001 From: asvitkine Date: Sat, 8 Jul 2023 14:26:55 -0400 Subject: [PATCH] Fix duplicate key error with edit mode's unit deletion. (#11747) Fix duplicate key error with edit mode's unit deletion. This can happen if a unit chooser is brought up and the user manually selects both a transport and a unit loaded onto it. The unit loaded onto it will be added twice due to also being added as a dependent. --- .../main/java/games/strategy/triplea/ui/UnitChooser.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/game-app/game-core/src/main/java/games/strategy/triplea/ui/UnitChooser.java b/game-app/game-core/src/main/java/games/strategy/triplea/ui/UnitChooser.java index cb56e789abe..13c3cda8ecd 100644 --- a/game-app/game-core/src/main/java/games/strategy/triplea/ui/UnitChooser.java +++ b/game-app/game-core/src/main/java/games/strategy/triplea/ui/UnitChooser.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -344,11 +345,12 @@ public Collection getSelected() { * killed). */ public List getSelected(final boolean selectDependents) { - final List selectedUnits = new ArrayList<>(); + // Use a Set to avoid duplicates in the case where dependents are also manually selected. + final var selectedUnits = new HashSet(); for (final ChooserEntry entry : entries) { addToCollection(selectedUnits, entry, entry.getFinalHit(), selectDependents); } - return selectedUnits; + return new ArrayList<>(selectedUnits); } /** Only applicable if this dialog was constructed using multiple hit points. */