博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
String 源码
阅读量:4217 次
发布时间:2019-05-26

本文共 1275 字,大约阅读时间需要 4 分钟。

String 类 的hashCode方法

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

/**     * Returns a hash code for this string. The hash code for a     * {@code String} object is computed as     * 
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]     * 
* using {@code int} arithmetic, where {@code s[i]} is the * ith character of the string, {@code n} is the length of * the string, and {@code ^} indicates exponentiation. * (The hash value of the empty string is zero.) * * @return a hash code value for this object. */ public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h; }

来个例子

/** * Created by tiantao on 2018/6/13. */public class StringTest{    public static void main(String[] args) {        //49        System.out.println("1".hashCode());                //49 * 31 + 49 = 1568        System.out.println("11".hashCode());                //49 * 31 * 31 + 49 * 31  + 49 = 48657        System.out.println("111".hashCode());                //49 * 31 * 31 + 50 * 31  + 51 = 48690        System.out.println("123".hashCode());    }}

转载地址:http://bhnmi.baihongyu.com/

你可能感兴趣的文章
mysql数据库主从同步的问题解决方法
查看>>
QC数据库表结构
查看>>
测试工具厂商的编程语言什么时候“退休”?
查看>>
资源监控工具 - Hyperic HQ
查看>>
LoadRunner中Concurrent与Simultaneous的区别
查看>>
SiteScope - Agentless监控
查看>>
欢迎加入【亿能测试快讯】邮件列表!
查看>>
为什么我们的自动化测试“要”这么难
查看>>
LoadRunner性能脚本开发实战训练
查看>>
测试之途,前途?钱途?图何?
查看>>
通过FTP服务的winsockes录制脚本
查看>>
LRwinsocket协议测试AAA服务器
查看>>
Net远程管理实验
查看>>
反病毒专家谈虚拟机技术 面临两大技术难题
查看>>
几种典型的反病毒技术:特征码技术、覆盖法技术等
查看>>
Software Security Testing软件安全测试
查看>>
论文浅尝 | 通过共享表示和结构化预测进行事件和事件时序关系的联合抽取
查看>>
廖雪峰Python教程 学习笔记3 hello.py
查看>>
从内核看epoll的实现(基于5.9.9)
查看>>
python与正则表达式
查看>>