SOURCE CODE
java.lang.Integer文件里,判断一个int的位数
计算十进制的位数
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
99999999, 999999999, Integer.MAX_VALUE };
// Requires positive x
static int stringSize(int x) {
for (int i=0; ; i++)
if (x <= sizeTable[i])
return i+1;
}HashMap.put
默认equals方法是比较地址return (this == obj); hashcode方法是native方法,注释是说把对象地址转换成integer。
实现equals不实现hashcode会导致equals判定相等的两个对象放在map里是不同的key。
两个key是否相等
(p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k))))
hashcode判相等貌似只在散列表里用到?普通的调a.equals(b)不会管hashcode?
由此可见先比hash(hashcode)再比equals,java只强制1. 根据equals()进行比较,相等的两个对象,hashCode()的值也必须相同; 2. 根据equals()进行比较,不相等的两个对象,hashCode()的值可以相同,也可以不同;
hashcode & equals
默认equals方法是比较地址return (this == obj); hashcode方法是native方法,注释是说把对象地址转换成integer。
hashcode是否真的用地址?
官方注释里说 hashcode方法默认是把对象地址转换成integer,但是有人分析实际的实现不是这样
hashcode是否从创建开始就保持不变?
官方注释里说 只要equals用来比较的信息不变,hashcode就应该不变。而且无论如何都不保证两次运行应用的hashcode相同。
在使用hash类型的集合时,最好保证用作key的对象不变。
When using a hash-based Collection or Map such as HashSet, LinkedHashSet, HashMap, Hashtable, or WeakHashMap, make sure that the hashCode() of the key objects that you put into the collection never changes while the object is in the collection. The bulletproof way to ensure this is to make your keys immutable, which has also other benefits.
判断两个对象是否相等 java.util.Collections#eq
乘以100
想半天没想明天为什么((q << 6) + (q << 5) + (q << 2))就是乘100,原来这个就相当于q * (2^6 + 2^5 + 2^2) = q * 100
把两位数转为char
Last updated
Was this helpful?