How do I calculate a good hash code for a list of strings?

后端 未结 11 1597
我寻月下人不归
我寻月下人不归 2020-12-01 02:34

Background:

  • I have a short list of strings.
  • The number of strings is not always the same, but are nearly always of the order of a “handful”
  • I
11条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 03:39

    Your first option has the only inconvenience of (String1, String2) producing the same hashcode of (String2, String1). If that's not a problem (eg. because you have a fix order) it's fine.

    "Cat all the string together then get the hashcode" seems the more natural and secure to me.

    Update: As a comment points out, this has the drawback that the list ("x", "yz") and ("xy","z") would give the same hash. To avoid this, you could join the strings with a string delimiter that cannot appear inside the strings.

    If the strings are big, you might prefer to hash each one, cat the hashcodes and rehash the result. More CPU, less memory.

提交回复
热议问题