Q:

Java Map with Set as Value. How to get all values for a given key?

I need your help. I have this method:
Set<Games> getGames(Player player) {
    Map<User, Set<Games>> map = repository.getGamesMap();
    Set<Games> result = new HashSet<>();
    for (Map.Entry<Player, Set<Games>> entry : map.entrySet()) {
        if (entry.getKey().equals(player)) {
            Set<Games> games = entry.getValue();
            result.addAll(games);
        }
    }
    return result;
}

The outcome is that nothing is added to result Set. If I skip the if condition, everything is added (games of all players, not only of a given one). 
If I try something like: 
return newHashSet(repository.getGamesMap().get(user));

I get null pointer when testing, even though games are added. So I probably have to check the key for each entry and that's what I'm trying to do.

A:

The problem is probably in the if statement, as you correctly pointed out. 
It seems that you have not overridden .equals() method inside the Player class. 
If you don't want to do that, you can change the if statement to something like:
if (entry.getKey().getName().equals(player.getName())) {

assuming that the Player class has a method getName() returning a String and that you want to compare two Player instances based on this string equality. 
You can adapt this equality condition either inside the if statement (as above), or inside the .equals() method of the Player (also overriding the hashCode() method, as Andreas commented). 
If you did override the equals() method, but it still doesn't work, try to debug this method or just print when it returns true and when false. 

