I have a SplayTreeMap
of players with their levels, and I wanna print a ranking of these players based on their level.
When I use .forEach
to iterate over this SplayTreeMap
, it ignores the players with the same level (means that while looping over SplayTreeMap
, it doesn't take into consideration values with duplicated keys).
Here is my code:
SplayTreeMap<int, String> map = SplayTreeMap<int, String>();
map[5] = 'player1';
map[2] = 'player2';
map[6] = 'player3';
map[7] = 'player4';
map[7] = 'player5';
map[7] = 'player6';
map.forEach((level, player) {
print('$player -> $level');
});
Here is the output of this code:
player2 -> 2
player1 -> 5
player3 -> 6
player6 -> 7
So I'm asking myself why it doesn't print player4
and player5
.
If there is no solution to this, what's the best alternative to SplayTreeMap
, to have a map, that's sorted based on its keys
A
SplayTreeMap
is a type ofMap
, andMap
s cannot have duplicate keys:(There are
Map
-like classes such aspackage:quiver
'sMultimap
, but they cannot derive fromMap
since they must provide a different signature for some methods (e.g.operator []
).)If you want a
SplayTreeMap
with multiple values for a single key, then you should storeList
s as the values. For example:Prints:
Note that the above implementation allows duplicate players within each level. If you don't want that, you can use a
Set<String>
instead of aList<String>
, or you can use aSplayTreeSet<String>
if you want the players to be sorted within each level.