awk 实现java string的 hashcode 算法
2022/6/6 1:23:17
本文主要是介绍awk 实现java string的 hashcode 算法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
java string 计算原理 code
public int hashCode() { int i = this.hash; if ((i == 0) && (this.value.length > 0)) { char[] arrayOfChar = this.value; for (int j = 0; j < this.value.length; ++j) i = 31 * i + arrayOfChar[j]; this.hash = i; } return i; }
java 测试code
public class test_hashcode { public static void main(String args[]) { String Str = new String("www.runoob.com"); System.out.println(Str+" ,字符串的哈希码为 :" + Str.hashCode() ); char a_ ; a_ = 'a'; int res ; res = 1+ a_ System.out.println("1+a :" + res ); } }
awk code
awk -f ./hashcode_test.awk
hashcode_test.awk
BEGIN { for (ii = 0; ii < 256; ++ii) { ch = sprintf("%c", ii); ascii[ch] = ii; } h = 0 ; strings="www.runoob.com" if(h ==0 && length(strings)>0) { for (i=1;i<= length(strings) ; ++i) { j=ascii[substr(strings,i,1)] print h " * 31 +" j |& "bc" "bc" |& getline var2 #h=31*h+j h=var2 } } print "(" h " +2147483648)%4294967296-2147483648 " |& "bc" "bc" |& getline Result print strings " , hashcode " Result }
实现结果
awk 结果
java 结果
oracle 函数实现 ,验证就不贴了
CREATE OR REPLACE FUNCTION HASHCODE(STR IN VARCHAR2) RETURN INTEGER IS RESULT INTEGER := 0; BEGIN FOR I IN 1 .. LENGTH(STR) LOOP RESULT := 31 * RESULT + ASCII(SUBSTR(STR, I, 1)); END LOOP; RESULT := MOD((RESULT + 2147483648), 4294967296) - 2147483648; RETURN(RESULT); END HASHCODE;
awk function
function hashcode(strings) { for (ii = 0; ii < 256; ++ii) { ch = sprintf("%c", ii); ascii[ch] = ii; } h = 0 ; if(h ==0 && length(strings)>0) { for (i=1;i<= length(strings) ; ++i) { j=ascii[substr(strings,i,1)] print h " * 31 +" j |& "bc" "bc" |& getline var2 h=var2 #print i","substr(strings,i,1) " , " j" , " h } } print "(" h " +2147483648)%4294967296-2147483648 " |& "bc" "bc" |& getline Result #print strings " , hashcode " Result return Result } ## test hashcode BEGIN { a ="www.runoob.com" Hcode= hashcode(a) print a " , hashcode " Hcode }
这篇关于awk 实现java string的 hashcode 算法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南