Nested Maps or combined keys in java

前端 未结 6 1673
一个人的身影
一个人的身影 2020-12-17 21:58

I need a Map to make a cache in Java for same values that I have two String keys. My question it\'s better to make nested Maps (one for each key) or make some type of custom

6条回答
  •  一个人的身影
    2020-12-17 22:52

    Using combined keys seems more logical to me, and easier to use (you don't have to worry about the nested map being instancied).

    About the combination of the keys, use a custom class. It makes more sense semantically, and prevents colisions if you have similar strings. Such a colision could appear if you have the key ["ab", "c"] and the key ["a", "bc"].

    Remember when you write your custom class, you need to write the equals and hashCode methods correctly, or your cache might not work (and might suffer performance issues as well).

提交回复
热议问题