最近工作碰到这么个小问题,分享一下:
java中我们常见的用来分片的方法就是取模,一般的方法就是
Math.abs(Long.valueOf(value).hashCode()) % shardingBase
在我们用一个脚本语言(例如这次的Python)去维护程序的时候,有时候我们需要一个同样的方法
现在问题来了,这么简单的一个方法怎么用Python来表示?
难道是:
long(value) % shardingbase
最后运行发现,基本不太一样。
我们看java在Long的hashcode()方法实现代码可以模仿出这个逻辑
在java8源码中,Long.valueof()源码核心代码如下:
final int offset = 128; if (l >= -128 && l <= 127) { // will cache return LongCache.cache[(int)l + offset]; } return new Long(l); //做了一个缓存,在此没大意义 private static class LongCache { private LongCache(){} static final Long cache[] = new Long[-(-128) + 127 + 1]; static { for(int i = 0; i < cache.length; i++) cache[i] = new Long(i - 128); } }
我们发现这个方法用Python来写基本没什么难度。没什么区别,主要区别看来就是在Java的hashCode()方法实现了,这个方法源码如下:
public int hashCode() { return Long.hashCode(value); } public static int hashCode(long value) { return (int)(value ^ (value >>> 32)); }
看来我们的主要任务就是把值右移动32位然后和本身做异或操作。因为我的问题是id不可能为负数,所以有符号右移和无符号右移再这对我没什么区别,这个问题好像也不大。这个地方的主要问题就是这个强转。Python中不存在int溢出的情况,因为他会自动将他转成一个long类型存放,所以,数子大一些的话用Python写出来的肯定不会是负数,结果肯定跟java不一样。所以这个时候Python应该这样写:
def longToInt(value): assert isinstance(value, (int, long)) return int(value & sys.maxint) def int_overflow(val): maxint = 2147483647 if not -maxint-1 <= val <= maxint: val = (val + (maxint + 1)) % (2 * (maxint + 1)) - maxint - 1 return val //Python写法 abs(int_overflow(longToInt(id ^ (id >> 32)))) % 200 //java写法 Math.abs(Long.valueOf(id).hashCode()) % 200
相关推荐
安装npm install string-hashcode 例子var hashCode = require ( 'string-hashcode' ) ;var s = 'abc' ;console . log ( s . hashCode ) ; // undefinedvar code = hashCode ( s ) ;console . log ( s . hashCode ) ...
单页后退前进刷新 jquery.hash.min.js 和 jquery.ba-hashchange.min.js
hashcode的作用.doc hashcode的作用.doc hashcode的作用.doc
而我有个类,这个类有个字段叫ID,我要把这个类存放在以上8个位置之一,如果不用hashcode而任意存放,那么当查找时就需要到这八个位置里挨个去找,或者用二分法一类的算法。 但如果用hashcode那就会使效率提高很多
java中Hashcode的作用
举例分析 equals 和 hashcode 方法,hashcode应该怎么样生成 8个基本类型与基本对象的比较:byte与Byte shot与Short int与Integer long与Long float与Float double与Double char与Character
of abstraction ........................ 15 An object has an interface ........................... 17 An object provides services ................... 18 The hidden implementation .................... ...
Java重写equals同时需要重写hashCode的代码说明,以及如何重写hashCode方法,此代码演示按照effective java书籍说明的重写思路。代码中演示了使用集合存储对象,并且对象作为key,需重写equals和hashCode.
实际上,hashcode根本不能代表object的内存地址。
如何正确实现Java中的HashCode共6页.pdf.zip
深入 HashCode 方法~~~~~
1.2.4. Collection of values ................................................................................ 17 1.2.5. Bi-directional associations ........................................................
Google HashCode只是为了好玩 2021年在线资格 我们首次参与了解决哈希码2021的在线认证问题"Traffic signaling"工作。 我们的结果是8,446,233点,而最高分是10,586,135 。 我们希望改善结果,甚至安排我们之间的小...
equals()和hashcode()这两个方法都是从object类中继承过来的。当String 、Math、还有Integer、Double。。。。等这些封装类在使用equals()方法时,已经覆盖了object类的equals()方法.
1.1.5. 用 Maven 构建 .................................................. 9 1.1.6. 启动和辅助类 .................................................... 9 1.1.7. 加载并存储对象 ................................
PPT浅析hashcode定义和作用;和简单的代码演示PPT.很简单的
Promise.pure(Integer.valueOf(string.hashCode()))); System.out.println("Main thread example2"); int hashCode = promise2.get(); System.out.println("HashCode = " + hashCode); 通过使用承诺
hashcode是用来查找的,如果你学过数据结构就应该知道,在查找和排序这一章有……