Java: Composite key in hashmaps

前端 未结 9 690
太阳男子
太阳男子 2020-12-24 12:53

I would like to store a group of objects in a hashmap , where the key shall be a composite of two string values. is there a way to achieve this?

i can simply concate

9条回答
  •  暖寄归人
    2020-12-24 13:34

    I have a similar case. All I do is concatenate the two strings separated by a tilde ( ~ ).

    So when the client calls the service function to get the object from the map, it looks like this:

    MyObject getMyObject(String key1, String key2) {
        String cacheKey = key1 + "~" + key2;
        return map.get(cachekey);
    }
    

    It is simple, but it works.

提交回复
热议问题