4

I have a map that contains a user entered remote machine name, and a user entered name on the host for a program running there. Don't want duplicate entries because the user typed upper case once then entered the same name in lowercase later.

Proto:

map<string, string> host_and_name = 1;

When storing, ideally I'd be able to use this method, because the map is marked case sensitive. Not the default behavior though, and don't see a way to decorate otherwise. Hoping I'm missing something.

cache.putHostAndHame( hostName, strategyName );

Aware I could iterate all map values and only put if I don't find an equalsCaseInsenstive match. I'll end up doing that if I have to, seems a bit brute force though.

Evan
  • 2,441
  • 23
  • 36
  • 3
    Do you need to preserve the casing? If not, you could always store them as all lowercase when placing them in so it is easy to check after. – Nexevis May 16 '19 at 18:33
  • You have no options in the behaviour of the map. To all intents and purposes, the map behaves like a regular LinkedHashMap. If you want "case insensitive" keys, you have to handle that yourself by normalizing the casing before putting/getting. – Andy Turner May 16 '19 at 18:41
  • Ideally, I do need to preserve case. Both the key and value are displayed back to the user, and clobbering case makes the strings much harder for them to read. Typically, these are multiple words with no space between for business reasons. Should have mentioned that first, but good question Nexevis. – Evan May 17 '19 at 13:17

1 Answers1

1

if you need to validate and put in your host_and_name map you can convert the all characters to lowercase and whenever you put every host or name it will get all the characters to lower case.

like below

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String str = "My Sample Text";

      System.out.print("Return Value :");
      System.out.println(str.toLowerCase());
   }
}
  • output

Return Value :my sample text

and also you can put it on your map and make a foreach loop to check host or name is already in the map. check this

I hope you get the idea. if there any question comment below. thanks.

Also this question have a good answers what you need to implement.

Jamith NImantha
  • 1,999
  • 2
  • 20
  • 27
  • 2
    There is (very nearly) never a reason to write `new String("...")`. Just use `"..."` instead. – Andy Turner May 16 '19 at 18:43
  • This is a good idea, unfortunately, as mentioned in the comments above, it's not ideal to discard case as these values are displayed back to the user. At the moment, I iterating, not modifying the map if both sides are equal, removing the value then putting if the key exists already. Based on Andy Turner's comment that there is no option for case insensitive keys, it seems you've highlighted both options available to me though. – Evan May 17 '19 at 13:20