
给我看看下面java代码!关于容器接口的。问题出在HashSet中的remove()无法正确移除已经存在的元素.
packagecom.ml.collection;importjava.util.*;className{privateStringfristName,lastName;...
package com.ml.collection;
import java.util.*;
class Name {
private String fristName,lastName;
Name(String fristName,String lastName) {
this.fristName = fristName;
this.lastName = lastName;
}
public String toString() {
return "fristName: " + fristName +" "+"lastName: " +lastName;
}
public boolean equals(Object obj) {
if(obj instanceof Name) {
Name name = (Name)obj;
return ((fristName.equals(name.fristName))&&(lastName.equals(name.lastName)));
}
else {
System.out.println("asfasf");
return super.equals(obj);
}
}
public int hasCode() {
return fristName.hashCode();
}
}
public class TestCollection {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c = new HashSet(); //无序的,不可添加相同元素,用HashSet初始c
Name n1 = new Name("mo","li");
Name n2 = new Name("mo","li");
System.out.println(n1.hasCode()+" "+n2.hasCode()); //2个对象哈希码一样
System.out.println(c.add(n1));
System.out.println(c.remove(n2)); //总是返回false,为什么啊?我已经重写了Object里 //面的equals和hashCode方法了
System.out.println(c);
System.out.println(); //换行
Collection c1 = new ArrayList(); //有序的,可以重复,用ArrayList初始c1
c1.add(new Name("san","li"));
System.out.println(c1.add(new Name("san","li")));//添加成功
c1.add("hello world!!!");
System.out.println(c1.add("hello world!!!")); //添加成功
System.out.println(c1.remove(new Name("san","li"))); //成功移除
System.out.println(c1);
}
}
/*main()中的2个测试区别在于Collection 的引用指向了不同的 【类】的对象,前 者无法正常移除,而后者却可以。
输出结果:*/
3490 3490 // 哈希吗一样,说明n1与n1在Collection中是相同的元素
true //成功添加 元素对象n1
false // 对于已经存在的元素移除失败了.
[fristName: mo lastName: li] //容器中还有这个元素
true
true
true
[fristName: san lastName: li, hello world!!!, hello world!!!] 展开
import java.util.*;
class Name {
private String fristName,lastName;
Name(String fristName,String lastName) {
this.fristName = fristName;
this.lastName = lastName;
}
public String toString() {
return "fristName: " + fristName +" "+"lastName: " +lastName;
}
public boolean equals(Object obj) {
if(obj instanceof Name) {
Name name = (Name)obj;
return ((fristName.equals(name.fristName))&&(lastName.equals(name.lastName)));
}
else {
System.out.println("asfasf");
return super.equals(obj);
}
}
public int hasCode() {
return fristName.hashCode();
}
}
public class TestCollection {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c = new HashSet(); //无序的,不可添加相同元素,用HashSet初始c
Name n1 = new Name("mo","li");
Name n2 = new Name("mo","li");
System.out.println(n1.hasCode()+" "+n2.hasCode()); //2个对象哈希码一样
System.out.println(c.add(n1));
System.out.println(c.remove(n2)); //总是返回false,为什么啊?我已经重写了Object里 //面的equals和hashCode方法了
System.out.println(c);
System.out.println(); //换行
Collection c1 = new ArrayList(); //有序的,可以重复,用ArrayList初始c1
c1.add(new Name("san","li"));
System.out.println(c1.add(new Name("san","li")));//添加成功
c1.add("hello world!!!");
System.out.println(c1.add("hello world!!!")); //添加成功
System.out.println(c1.remove(new Name("san","li"))); //成功移除
System.out.println(c1);
}
}
/*main()中的2个测试区别在于Collection 的引用指向了不同的 【类】的对象,前 者无法正常移除,而后者却可以。
输出结果:*/
3490 3490 // 哈希吗一样,说明n1与n1在Collection中是相同的元素
true //成功添加 元素对象n1
false // 对于已经存在的元素移除失败了.
[fristName: mo lastName: li] //容器中还有这个元素
true
true
true
[fristName: san lastName: li, hello world!!!, hello world!!!] 展开
展开全部
方法名错误了public int hasCode() { 错误 少了一个h
应该覆盖public int hashCode() {方法。修改后执行正常。
应该覆盖public int hashCode() {方法。修改后执行正常。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询