HashMap loses order making solution potentially wrong

The expected output has the groups in a specific order, but as the solution uses a HashMap to store the results the order they are output in when using the foreach loop is not guarenteed - thus the solution is potentially wrong.

Either the expected output should be changed to accept the groups in any order, or the solution should be changed to use a map with a specific order.