`
xtwxgh
  • 浏览: 4263 次
  • 来自: ...
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

这个题真实变态 怎么改呐!!!

阅读更多
/**补充完该类,不修改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());
}
}
分享到:
评论
15 楼 抛出异常的爱 2007-05-10  
重写jvm中的string
equles(){
return true;
}
为什么不能满足?
14 楼 arthurln 2007-05-10  
jdk1.5是实现不了,因为:
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”又不是同一个引用,所以无法满足。
13 楼 kdekid 2007-05-10  
在 Effective Java 里面提过这种应该被禁止的做法。正常情况下,是不应该 override hashCode 和 equals 的
12 楼 javatestscm 2007-05-10  
这个是想考HASHCODE()和EQUALS(),不过题出的有点问题。在JDK1.5下是出不来结果的。
if (e.hash == hash && eq(k, e.key)) 
                return e.value;

11 楼 dovecat 2007-05-10  
...equals和hashcode对于想放入Collection等东东的对象要配套写...
10 楼 抛出异常的爱 2007-05-10  
ddy
忘记写这个方法了。
equals()
9 楼 xtwxgh 2007-05-10  
通过统一定义equals()和hashCode(),
可以提升类作为基于散列的集合中的关键字的使用性
怎样写这两个方法呐?
8 楼 ddandyy 2007-05-10  
走到了   是这句

if (e.hash == hash && eq(k, e.key))
7 楼 ddandyy 2007-05-10  
翻了一下 没找到类似语句

    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
6 楼 抛出异常的爱 2007-05-10  
if(!(obj instanceof Test)) return false;
大约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;
 } 
5 楼 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???????
4 楼 抛出异常的爱 2007-05-10  
hashCode(){
return name.hashCode();
}
3 楼 xtwxgh 2007-05-10  
请问能详细一点吗?
是重写Student类的hashCode()方法吗?
2 楼 Godlikeme 2007-05-10  
hashcode
1 楼 xtwxgh 2007-05-10  
SORRY!  System.out.print(p.keySet().iterator().next()); 应该去掉

相关推荐

Global site tag (gtag.js) - Google Analytics