野原新之楠
2019-04-06 15:53Java 关于如何override hashcode的问题
3个练习里分别要定义3中ADT(abstract data type)
1. FList (定义一个list)
2. FSet (定义一个set)
3. FMap(定义一个map)
在改写hashcode时候遇到了困难。前两个练习没有做关于hashcode的说明
我直接 return FList.size() 和 FSet.size() ps:我知道这样不对。。。。。
第3个练习里有了规定 瞬间不懂了。。。
If m1 and m2 are values of the FMap ADT, and
! (m1.equals(m2))
then m1.hashCode() is unlikely to be equal to m2.hashCode().
Note: The word "unlikely" will be interpreted as follows. For every type K and V, if both m1 and m2 are selected at random from a set of FMap values such that for every non-negative integer n and int value h the probability of a randomly selected FMap m having
n == m.size() is
P(n) = 1/(2^(n+1))
and for each key k such that m.containsKey(k) the probability that
h == k.hashCode() is at most 1/5
and for each value v such that v.equals(m.get(k)) the probability that
h == v.hashCode() is at most 1/5
and the three probabilities above are independent
then the probability of m1.hashCode() == m2.hashCode() when m1 and m2 are not equal is less than 40%.
这里面是要求我设计hashcode m1和m2相等概率小于40%?
- 点赞
- 回答
- 收藏
- 复制链接分享
1条回答
为你推荐
- hashcode()中 lineNumber是一个变量,怎么可以调用hashCode()方法?
- java
- java-ee
- 1个回答
- Java 内存泄漏问题
- 内存泄露
- 0个回答
- 多线程 java.util.ConcurrentModificationException 问题
- java
- 多线程
- 0个回答
- 新手学J2SE,遇到一个集合问题,搞不懂代码什么意思
- j2se
- 集合
- 0个回答
- 关于java动态代理的一个奇怪问题
- it技术
- 互联网问答
- IT行业问题
- 计算机技术
- 编程语言问答
- 0个回答