2007-05-10
这个题真实变态 怎么改呐!!!
/**补充完该类,不修改main方法,使得get()方法可以取到值*/
package test;
import java.util.HashMap;
import java.util.Map;
public class StudentTest {
private static final class Student {
private static String name;
public Student(String name) {
this.name = name;
}
}
public static void main(String[] args) {
Map p = new HashMap();
p.put(new Student("lily"), "sister");
System.out.println(p.get("lily"));
System.out.print(p.keySet().iterator().next());
}
}
package test;
import java.util.HashMap;
import java.util.Map;
public class StudentTest {
private static final class Student {
private static String name;
public Student(String name) {
this.name = name;
}
}
public static void main(String[] args) {
Map p = new HashMap();
p.put(new Student("lily"), "sister");
System.out.println(p.get("lily"));
System.out.print(p.keySet().iterator().next());
}
}
评论
抛出异常的爱
2007-05-10
重写jvm中的string
equles(){
return true;
}
为什么不能满足?
equles(){
return true;
}
为什么不能满足?
arthurln
2007-05-10
jdk1.5是实现不了,因为:
1、在hashmap中是的get方法中是这样写的:
e.hash = hash 可以通过重载student的hashCode()方法满足。
但是第二个条件,好像不能满足。
这里key是一个String “lily”,但是k是一个V型的数据。在String的equals方法中这样写的:
因为k既不是String对象,和”lily”又不是同一个引用,所以无法满足。
1、在hashmap中是的get方法中是这样写的:
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
return e.value;
e.hash = hash 可以通过重载student的hashCode()方法满足。
但是第二个条件,好像不能满足。
这里key是一个String “lily”,但是k是一个V型的数据。在String的equals方法中这样写的:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
因为k既不是String对象,和”lily”又不是同一个引用,所以无法满足。
kdekid
2007-05-10
在 Effective Java 里面提过这种应该被禁止的做法。正常情况下,是不应该 override hashCode 和 equals 的
javatestscm
2007-05-10
这个是想考HASHCODE()和EQUALS(),不过题出的有点问题。在JDK1.5下是出不来结果的。
if (e.hash == hash && eq(k, e.key))
return e.value;
dovecat
2007-05-10
...equals和hashcode对于想放入Collection等东东的对象要配套写...
抛出异常的爱
2007-05-10
ddy
忘记写这个方法了。
equals()
忘记写这个方法了。
equals()
xtwxgh
2007-05-10
通过统一定义equals()和hashCode(), 可以提升类作为基于散列的集合中的关键字的使用性 怎样写这两个方法呐?
ddandyy
2007-05-10
走到了 是这句
if (e.hash == hash && eq(k, e.key))
if (e.hash == hash && eq(k, e.key))
ddandyy
2007-05-10
翻了一下 没找到类似语句
刚开始怀疑是 indexFor 返回的不一样 可是test了一下 都是10
debug了一下
第一个情况 table里面是 Test$Student@32afca=sister
第二个情况 table里面是 lily=sister
public Object get(Object key) {
Object k = maskNull(key);
int hash = hash(k);
int i = indexFor(hash, table.length);
Entry e = table[i];
while (true) {
if (e == null)
return e;
if (e.hash == hash && eq(k, e.key))
return e.value;
e = e.next;
}
}
public Object put(Object key, Object value) {
Object k = maskNull(key);
int hash = hash(k);
int i = indexFor(hash, table.length);
for (Entry e = table[i]; e != null; e = e.next) {
if (e.hash == hash && eq(k, e.key)) {
Object oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, k, value, i);
return null;
}
刚开始怀疑是 indexFor 返回的不一样 可是test了一下 都是10
debug了一下
第一个情况 table里面是 Test$Student@32afca=sister
第二个情况 table里面是 lily=sister
抛出异常的爱
2007-05-10
if(!(obj instanceof Test)) return false;
大约map先作这件事
大约map先作这件事
public Object get(Object key) {
Object k = maskNull(key);
int hash = hash(k);
int i = indexFor(hash, table.length);
Entry e = table[i];
while (true) {
if (e == null)
return e;
if (e.hash == hash && eq(k, e.key))
return e.value;
e = e.next;
}
}
static int hash(Object x) {
int h = x.hashCode();
h += ~(h << 9);
h ^= (h >>> 14);
h += (h << 4);
h ^= (h >>> 10);
return h;
}
ddandyy
2007-05-10
private static final class Student {
private static String name;
public Student(String name) {
this.name = name;
}
public int hashCode() {
return this.name.hashCode();
}
}
试了一下
Map p = new HashMap();
Student tt = new Student("lily");
p.put(tt, "sister");
System.out.println(p.get("lily"));
null
Map p = new HashMap();
Student tt = new Student("lily");
p.put(("lily", "sister");
System.out.println(p.get("lily"));
sister
why???????
抛出异常的爱
2007-05-10
hashCode(){
return name.hashCode();
}
return name.hashCode();
}
xtwxgh
2007-05-10
请问能详细一点吗?
是重写Student类的hashCode()方法吗?
是重写Student类的hashCode()方法吗?
Godlikeme
2007-05-10
hashcode
xtwxgh
2007-05-10
SORRY! System.out.print(p.keySet().iterator().next()); 应该去掉
- 浏览: 2060 次

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
这个题真实变态 怎么改呐 ...
重写jvm中的string equles(){ return true; } 为 ...
-- by 抛出异常的爱 -
这个题真实变态 怎么改呐 ...
jdk1.5是实现不了,因为: 1、在hashmap中是的get方法中是这样写的 ...
-- by arthurln -
这个题真实变态 怎么改呐 ...
在 Effective Java 里面提过这种应该被禁止的做法。正常情况下,是不 ...
-- by kdekid -
这个题真实变态 怎么改呐 ...
这个是想考HASHCODE()和EQUALS(),不过题出的有点问题。在JDK1 ...
-- by javatestscm -
这个题真实变态 怎么改呐 ...
...equals和hashcode对于想放入Collection等东东的对象要 ...
-- by dovecat






评论排行榜