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

后端 未结 11 1571
我寻月下人不归
我寻月下人不归 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:20

    Standard java practise, is to simply write

    final int prime = 31;
    int result = 1;
    for( String s : strings )
    {
        result = result * prime + s.hashCode();
    }
    // result is the hashcode.
    

提交回复
热议问题