Nested Maps or combined keys in java

前端 未结 6 1663
一个人的身影
一个人的身影 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:58

    it's better to make nested Maps (one for each key) or make some type of custom key maked with the two Strings?

    A single hashtable with a composite key type may have better locality of reference. It will almost certainly take less memory and be a bit faster, though how much depends on your application.

    Designing a custom hash function can be tricky, but you can start with a function that just takes the already-defined hashes of the composite key's members and XORs them together. You could also take the string route, but then make sure you concatenate the members with some marker in between them (such as ":") that is unlikely to occur in them.

提交回复
热议问题