DualHashBidiMap 和 getKey 方法
DualHashBidiMap and getKey method
我对 DualHashBidiMap 和 getKey 方法有疑问。
我正在使用 Commons Collections 4.1
containsKey 方法 returns 对特定键为 true,但 getKey 方法 returns null相同的键;
Key Class 有一个 SuperClass,其中 equals 和 hashcode 方法被覆盖以匹配 id属性.
主要Class
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
DualHashBidiMap<Observed, Object> map=new DualHashBidiMap<Observed,Object>();
Task t66=new Task();
t66.setId(66);
map.put(t66, "Task66");
Task tFetch=new Task();
tFetch.setId(66);
System.out.println("tFetch present:"+map.containsKey(tFetch));
System.out.println("tFetch Object:"+map.getKey(tFetch));
}
}
这是输出
tFetch present:true
tFetch Object:null
键Class
public class Task extends Observed{
public void m1(){
System.out.println("Method called !!");
}
}
键超Class
public class Observed extends Observable{
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public boolean equals(Object obj) {
boolean retValue=false;
Observed t=(Observed) obj;
if(t.getId().equals(this.getId())) retValue=true;
return retValue;
}
@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash + (this.getId() != null ? this.getId().hashCode() : 0);
hash = 53 * hash + this.getId();
return hash;
}
}
谢谢大家..
您正在尝试获取地图中不存在的值的键。也许你想做如下
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
DualHashBidiMap<Observed, Object> map=new DualHashBidiMap<Observed,Object>();
Task t66=new Task();
t66.setId(66);
map.put(t66, "Task66");
Task tFetch=new Task();
tFetch.setId(66);
System.out.println("tFetch present:"+map.containsKey(tFetch));
// to get the key related to an object
System.out.println("tFetch Object:"+map.getKey("Task66"));
// to get a value related to a key
System.out.println("tFetch Object:"+map.get(tFetch));
}
}
我对 DualHashBidiMap 和 getKey 方法有疑问。
我正在使用 Commons Collections 4.1
containsKey 方法 returns 对特定键为 true,但 getKey 方法 returns null相同的键;
Key Class 有一个 SuperClass,其中 equals 和 hashcode 方法被覆盖以匹配 id属性.
主要Class
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
DualHashBidiMap<Observed, Object> map=new DualHashBidiMap<Observed,Object>();
Task t66=new Task();
t66.setId(66);
map.put(t66, "Task66");
Task tFetch=new Task();
tFetch.setId(66);
System.out.println("tFetch present:"+map.containsKey(tFetch));
System.out.println("tFetch Object:"+map.getKey(tFetch));
}
}
这是输出
tFetch present:true
tFetch Object:null
键Class
public class Task extends Observed{
public void m1(){
System.out.println("Method called !!");
}
}
键超Class
public class Observed extends Observable{
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public boolean equals(Object obj) {
boolean retValue=false;
Observed t=(Observed) obj;
if(t.getId().equals(this.getId())) retValue=true;
return retValue;
}
@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash + (this.getId() != null ? this.getId().hashCode() : 0);
hash = 53 * hash + this.getId();
return hash;
}
}
谢谢大家..
您正在尝试获取地图中不存在的值的键。也许你想做如下
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
DualHashBidiMap<Observed, Object> map=new DualHashBidiMap<Observed,Object>();
Task t66=new Task();
t66.setId(66);
map.put(t66, "Task66");
Task tFetch=new Task();
tFetch.setId(66);
System.out.println("tFetch present:"+map.containsKey(tFetch));
// to get the key related to an object
System.out.println("tFetch Object:"+map.getKey("Task66"));
// to get a value related to a key
System.out.println("tFetch Object:"+map.get(tFetch));
}
}