Search code examples
javaconcurrencyhashmapconcurrenthashmapconcurrentskiplistmap

Insensetive thread safe Map doesnt work properly


I have ConcurrentSkipListMap with insensetive key feaure:

public static ConcurrentMap<String, GooglePlayGame> games = new ConcurrentSkipListMap<String, GooglePlayGame>(String.CASE_INSENSITIVE_ORDER);

If i run my app first time and will add to map user request, f.e. i completed --games.put("Oddmar", ...) --, if user after this will request --/game oddmar (odDmar or whatever)-- it will works, but.. I always load my map from json file at start in static block:

static {
        TypeFactory typeFactory = mapper.getTypeFactory();
        MapType mapType = typeFactory.constructMapType(ConcurrentSkipListMap.class, String.class, GooglePlayGame.class);
        try {
            games = mapper.readValue(new File(
                    "gpgames_library.json"), mapType);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

If after "restart" user will try /game oddmar, it will not works - only /game Oddmar. But why? If in app i always read map from my variable (not from file).


Solution

  • When your "TypeFactory" creates the map, it doesn't use CASE_INSENSITIVE_ORDER but the natural order. I don't know what your "TypeFactory" is but one way to sidestep the problem is to read the JSON into a temporary value and then using putAll to add what was read from the JSON file into your games map.

    Map<String, GooglePlayGame> temporary = mapper.readValue(new File(
              "gpgames_library.json"), mapType);
    games.putAll(temporary);