我正在使用地图并希望将值对象用作地图键..并将列表用作值。值对象有 2 个属性名字,名字..如果两个属性都与同一个地图中的某个键匹配,我想返回 map.containsKey() 为真..
我尝试使用如下比较器
public class comaparatorEx implements Comparator<Test>{
public static void main(String args[]){
Map m= new HashMap<Test,List<String>>();
Test t = new Test();
t.setFirstname("vamsi");
t.setSecondname("priya");
List descriptionList=new ArrayList();
descriptionList.add("description1");
m.put(t, descriptionList);
Test t2 = new Test();
t2.setFirstname("vamsi");
t2.setSecondname("priya");
if(m.containsKey(t2)){
System.out.println("user found");
}
}
public int compare(Test o1, Test o2) {
if((o1.firstname.equals(o2.firstname) )&& o1.secondname.equals(o2.secondname))
return 0;
else return 1;
}
}
这是我正在使用的值对象
public class Test {
String firstname;
String secondname;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getSecondname() {
return secondname;
}
public void setSecondname(String secondname) {
this.secondname = secondname;
}
}
但它对我来说返回错误..请帮助我..提前谢谢