Creating a hash from several Java string objects

后端 未结 5 1315
情深已故
情深已故 2021-01-17 14:35

What would be the fastest and more robust (in terms of uniqueness) way for implementing a method like

public abstract String hash(String[] values);
         


        
5条回答
  •  遇见更好的自我
    2021-01-17 14:56

    Definitely don't use plain addition due to its linearity properties, but you can modify your code just slightly to achieve very good dispersion.

    public String hash(String[] values) {
      long result = 17;
      for (String v:values) result = 37*result + v.hashCode();
      return String.valueOf(result);
    }
    

提交回复
热议问题