I'm writing a script that creates a map from user nicknames to user IDs:
// Get client informations to map
for (Client c : api.getClients()) {
teamspeakUserInfos.put(c.getNickname(), c.getId());
}
Example mapping:
Nickname | 5
Username | 2
John |12
Here is my code for checking key presence in the map:
if (teamspeakUserInfos.containsKey(genUserNickname())) {
victimTeamspeakID = teamspeakUserInfos.get(genUserNickname());
break;
}
genUserNickname()
can return upper-, lower- or mixed-case nickname.
My question now is, how can I check if this nickname is used as a key - while ignoring case differences?
Thank you in advance!
You could do something similar:
public class ContainsKeyIgnoreCase {
public static void main(String[] args) {
Map<String, Integer> userInfo = new HashMap<String, Integer>();
userInfo.put("nickNamE", 1);
String nickName = "NiCKname";
System.out.println(containsKeyIgnoreCase(userInfo, nickName));
}
public static boolean containsKeyIgnoreCase(Map<String, Integer> map, String lookupKey) {
return map.keySet()
.stream()
.map(key -> key.equalsIgnoreCase(lookupKey))
.reduce(false, Boolean::logicalOr);
}
}