1

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!

GreenhouseVeg
  • 617
  • 5
  • 13
snorp
  • 47
  • 4
  • it would better to include that in the post. – Ousmane D. Dec 13 '18 at 19:21
  • 1
    What is a "character size"? – Andy Turner Dec 13 '18 at 19:32
  • Can you just store the nickname in the map as uppercase, and use `teamspeakUserInfos.containsKey(getUserNickname().toUpperCase())`? – Dave Drake Dec 13 '18 at 19:33
  • I suspect the answer you are looking for is to use `.put(c.getNickname().toLowercase(), ...)`, and then use `containsKey(genUserNickname(). toLowercase())` etc to retrieve from the map. – Andy Turner Dec 13 '18 at 19:34
  • @DaveDrake Sure, but I have to access them sometime and then it would be better if the writing is correct. – snorp Dec 13 '18 at 20:58
  • @AndyTurner That was wrong wording. I meant upper and lower case of the individual letters. `.toLowerCase()` unfortunately doesn't work ... theoretically this command only sets the searched string small but the comparison should only check the content of the string. – snorp Dec 13 '18 at 21:15

1 Answers1

0

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);
    }   
}
GreenhouseVeg
  • 617
  • 5
  • 13
  • Thanks for you fast replay! `Cannot infer type argument(s) for map(Function super T,? extends R>)` – snorp Dec 13 '18 at 23:49
  • @snorp I have added a complete code example to my answer, it worked for me (`true`is printed in the console). Your problem could be an Eclipse bug: https://stackoverflow.com/questions/42285370/cannot-infer-type-arguments-for-r-mapfunction-super-t-extends-r-in-so – GreenhouseVeg Dec 14 '18 at 08:58
  • Okay, thanks for the advice! I am currently using Version 4.9.0 of Eclipse and the bug was fixed with release 4.6.3 so I do not know why I still get this Error. EDIT: I will test your edited example above... EDIT2: It works like a charm, thank you very much. Now I get an error because of the string genUserNickname. `Local variable genUserNickname defined in an enclosing scope must be final or effectively final` But the whole thing is in a while loop. Anyway the problem I had first is solved thank you very much! – snorp Dec 14 '18 at 09:55