2013-12-11 53 views
1

我有一個HashMap,它存儲我創建的對象作爲鍵並映射到類似對象的ArrayList帶有ArrayLists值的HashMap在Get()調用返回空值

不過,我調用get方法,以及使用jGrasp的調試器,我可以清楚地看到,我正在使用get()關鍵存在,確實映射到array但唯一的價值,我可以得到的是一個null值。

這裏是我得到null價值。

public List<Entry> query(Record query) { 
    List<Entry> candList; 
    Entry key = new Entry(makeKey(query)); 

    candList = map.get(key); 

    return candList; 
} 

這裏是我從主店中填充HashMap的地方。

for(int i = 0; i < main.size(); i++) { 
    if(main.get(i).isActive()) { 
     values.clear(); 
     tmp = new Entry(main.get(i).record()); 
     key = new Entry(Record.make(tmp.entity(),tmp.relation(),wild)); 

     if(!map.containsKey(key)) { 
      for(int v = 0; v < main.size(); v++) { 
       value = main.get(v); 

       if(key.entity().equals(value.entity()) && key.relation().equals(value.relation())) { 
        values.add(value); 
       }       
      } 

      map.put(key,new ArrayList(values)); 
     } 
    } 
} 

Entry是一個包裝類,默認爲在其內的對象的方法equals(),在這裏。

public boolean equals(Object o){ 
    if(o == null){ 
     return false; 
    } 
    else if(o instanceof Record){ 
     Record r = (Record) o; 
     return this.entity.equals(r.entity) && this.relation.equals(r.relation) && this.property.equals(r.property); 
    } 

    else return false; 
} 

我也有一個散列碼寫在這裏的對象。

int h = 0; 
public int hashCode() { 

    int hash = h; 

    if(h != 0) 
    return hash; 

    String len = entity.concat(relation.concat(property)); 
    for(int i = 0; i < len.length(); i++) 
     hash = hash * 31 +(int)len.charAt(i); 

    return hash; 
} 

對於一個小澄清,所述Entry對象認爲包含三個不可改變String小號Record類型的對象,因此,其中hashCode方程來自。

有人要求進一步澄清,看看整個Entry類。

private static class Entry { 
    private static boolean active; 
    private Record rec; 

    public Entry(Record r){ 
     this.rec = r; 
     this.active = true; 
    }  

    public String entity() { 
     return rec.entity; 
    } 

    public String relation() { 
     return rec.relation; 
    } 

    public String property() { 
     return rec.property; 
    } 

    public Record record(){ 
     return this.rec; 
    } 

    public boolean isActive(){ 
    return this.active; 
    } 

    public void deactivate(){ 
    this.active = false; 
    } 

    public void activate(){ 
    this.active = true; 
    } 

    public boolean equals(Entry e) { 
     return this.rec.equals(e.record()); 
    } 

    public int hashCode() { 
     return this.rec.hashCode(); 
    } 

    public String toString() { 
     return rec.toString(); 
    } 
} 

有我HashMap發生一些碰撞,但我知道這不應該是太大的問題的。有任何想法嗎?

+2

什麼是Entry類的equals方法實現? – fmodos

+0

編輯表明。 – AKon

+0

'o instanceof Record' o永遠不會是實例記錄,它通過參數而不是記錄來接收和輸入oobject記錄 – fmodos

回答

0
public boolean equals(Object o){ 
    if(o == null){ 
     return false; 
    } 
    else if(o instanceof Record){ 
     Record r = (Record) o; 
     return this.entity.equals(r.entity) && this.relation.equals(r.relation) && this.property.equals(r.property); 
    } 

    else return false; 
} 

您的Entry等於方法可能有一些問題,關係的定義是什麼? 這個關係必須被覆蓋equals()和hashCode()

把你所有的代碼放在這裏,你的主要定義是什麼? 在你的代碼中有很多地方可能包含空指針錯誤