The preferred role is not checked properly.
Check this change:
The first loop is not properly changed, see the second loop.
Before the change:
String existingRole = userRoleMap.get(userEid);
After the change:
String existingRole = entry.getValue();
That's wrong !!
In the second loop the change is well done !!
The consecuence is that the preferred role is not calculated properly in the getUserRolesForGroup method.