无涯教程-Java - int hashCode()函数

89 阅读1分钟

此方法返回此字符串的hashCode。字符串对象的hashCode计算为-

s[0]*31^(n - 1) + s[1]*31^(n - 2) + ... + s[n - 1]

使用int算术,其中s [i]是字符串的第i个字符,n是字符串的长度,^表示幂。 (空字符串的哈希值为零。)

int hashCode() - 语法

public int hashCode()

int hashCode() - 返回值

  • 此方法返回此对象的hashCode值。

int hashCode() - 示例

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str=new String("Welcome to Learnfk.com");
      System.out.println("Hashcode for Str :" + Str.hashCode() );
   }
}

这将产生以下输出-

Hashcode for Str :1186874997

参考链接

www.learnfk.com/java/java-s…