KEY:
static private String name
static private String name
解决方案 »
- 运行包含junit测试的jar
- 页面跳转问题
- 我选 a c d
- java编码问题
- Swing组建 JTable 使用new DefalutTableModel (Vector,Vector) 出现 can't be cast to java.util.vector 这样的异常,请问为什么啊?
- IO问题,有经验的来看一哈啊
- 在网上找了个全英文的java源代码(包括字符),为什么我在记事本里修改字符,编译后出现乱码???
- 请教Order of initialization
- 如何实现多继承?
- 请我在哪里可以下载JSDK包,谢谢!
- 请问怎么才能用java在.txt文本上输入回车符.
- repaint()为什么出错
private Key instance = new Key();
private String name;
private Key() {
}
public Key getInstance() {
return instance;
}public void setName(string name) { this.name = name;}}
import java.util.HashMap;public class TestQuestion {
public static void main(String args[]) {
Map map = new HashMap();
map.put(new Key(5),new Integer(12));
Object o = map.get(new Key(5));
if (o instanceof Integer) {
System.out.println("got value = "+(Integer)o);
} else {
System.out.println("got no object");
}
}
}class Key {
private int name;
Key(int name){
this.name = name;
}
public boolean equals(Object o){
return (o instanceof Key)
&& (name == (((Key)o).name));
}
public int hashCode(){
return name;
}
}
以上可以正确的得到你想要的对象-----------------------------------------------------------
private Key instance = new Key();
private String name;
private Key() {
}
public Key getInstance() {
return instance;
}public void setName(string name) { this.name = name;}}
---------------------------------------------------
不好意思,忘了说明一点,不能修改调用Key这个类的构造方法,也就是必须用 new Key("参数")
但可以在Key的内部声明其他形式的构造方法,所以用 getInstance()方法也不行
Object o = map.get(new Key("TEST"));
-------------------------------------------------
你这两句new 了两个对象,虽然都是用的是"TEST",
但是两个对象的hashCode却不是一样的,在HashMap中
的get方法会先调用开始put进去的key的equals()和hashCode()
这两个方法,如果你是用你自己写的类来作key,那么你得重写
这两个方法,不然它们缺省是用Object的equals和hashCode,
就像你刚刚put时new了一个对象,get时又new了一个对象,
明明放进去了,怎么得不到呢?问题就在这里,所以一般
很少用自己写的类去做key的,可以用Integer或String来做key,
一旦你要用自己写的来做,那你得重新equals和hashCode这两个方法
你的代码我试过了,打印的结果还是
got no object
而不是
got value = 这段程序的最终目的就是想把放进去的匿名实例再取出来,但取出来的时候也是产生一个实例的过程
所以,要求只能修改Key这个类来实现类似
Key k = new Key("Test")
map.put(k,new Integer(12));
Object o = map.get(k);
这样的功能!
private String name;
public Key(String name) {
this.name = name;
}
public int hashCode() {
return name.hashCode();
}
public boolean equals(Object obj) {
return name.equals(((Key)obj).name);
}
}
import java.util.Map;
import java.util.HashMap;public class TestQuestion {
public static void main(String args[]) {
Map map = new HashMap();
map.put(new Key("TEST"),new Integer(12));
Object o = map.get(new Key(new String("TEST")));
if (o instanceof Integer) {
System.out.println("got value = "+(Integer)o);
} else {
System.out.println("got no object");
}
}
}class Key {
private String name;
Key(String name){
this.name = name;
}
public boolean equals(Object o){
return (o instanceof Key)
&& (name == (((Key)o).name));
}
public int hashCode(){
return name.hashCode();
}
}
----------------------------------------------
Object o = map.get(new Key(new String("TEST")));这样得出来的hashCode就不一样了
你的代码那个参数是int型的,hashCode()返回类型也是int型的啊
public int hashCode()
Returns a hash code for this string. The hash code for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.) Overrides:
hashCode in class Object
Returns:
a hash code value for this object.
See Also:
Object.equals(java.lang.Object), Hashtable
所以只要内容一样String的hashCode总是一样的。
import java.util.HashMap;public class TestQuestion {
public static void main(String args[]) {
Map map = new HashMap();
map.put(new Key("TEST"),new Integer(12));
Object o = map.get(new Key(new String("TEST")));
if (o instanceof Integer) {
System.out.println("got value = "+(Integer)o);
} else {
System.out.println("got no object");
}
}
}class Key {
private String name;
Key(String name){
this.name = name;
}
public boolean equals(Object o){
return (o instanceof Key)
&& (name == (((Key)o).name));
}
public int hashCode(){
return name.hashCode();
}
}
To : takecare(大厅)
你的代码执行结果并不打印出 got value = 12
public boolean equals(Object o){
return (o instanceof Key)
&& (name == (((Key)o).name));
}
name == (((Key)o).name这个能用 ==吗?class Key {
private String name;
Key(String name) {
this.name = name;
}
public boolean equals(Object o) {
return (o instanceof Key) && (name.equals(((Key) o).name));
}
public int hashCode() {
System.out.println("name-"+name+"--hash:"+name.hashCode());
return name.hashCode();
}
}
private String name;
Key(String name){
this.name = name;
}
public boolean equals(Object o){
return (o instanceof Key)
&& (name.equals(((Key)o).name));
}
public int hashCode(){
System.out.println(name.hashCode());
return name.hashCode();
}
}
成了嵌套调用?不会导致错误嘛!return (o instanceof Key)&& (name.equals(((Key)o).name));
意思不是反回String所在的地址位置吗?是 class Key == String name 的意思吗?
在 Java 里,无论什么对象,只要使用 new 语句就必然会产生一个真正的实例.而且你的这句本来就不对:
Object o = map.get(new Key(new String("TEST")));你自己都强制指定 JVM 去 new 一个 Key() ,怎么可能不产生一个新的实例呢?
要是不改写你写的 TestQuestion 类中的代码,你说的功能根本就是不可能实现的!!
除非你不用 Java 语言,呵呵!
Object o = map.get(new Key("TEST"));
难道你说上面这两语句的作用不一样,
new String("TEST") 和 "TEST"都是产生了一个新的字符串对象,只是它们在内存中的存储方式不太一样罢了!
Object o = map.get(new Key(new String("TEST")));
这句话的意思就是让JVM去new 一个新的Key实例,但我要的功能是在new Key这个实例时会检测是否已存在参数相同的实例,如果存在,就让这个新的也指向它,所以,用hashCode()是可以完成这样功能的!
至少我用下面的代码做了好多测试是正确的.最终的代码是这样的,这就可以达到我说的效果,也就是在产生 new Key("参数")时,只要参数不变,你产生再多次,都是一个实例.import java.util.Map;
import java.util.HashMap;public class TestQuestion {
public static void main(String args[]) {
Map map = new HashMap();
map.put(new Key("TEST"),new Integer(12)); Object o = map.get(new Key("TEST"));
if (o instanceof Integer) {
System.out.println("got value = "+(Integer)o);
} else {
System.out.println("got no object");
}
}
}
class Key {
private String name;
Key(String name) {
this.name = name;
}
public boolean equals(Object o) {
return (o instanceof Key) && (name.equals(((Key) o).name));
}
public int hashCode() {
System.out.println("name ==> "+name+" hash ==> "+name.hashCode());
return name.hashCode();
}
}
你所说的单态模式是可以完成唯一实例这个功能的,但我现在的代码是只能修改Key这个类,TestQuestion这个类不能做任何修改,也就是调用产生Key的时候必须用new Key("参数")这样的形式谢谢
fog628(发粪涂墙)
takecare(大厅)
二位的指点....