2014-05-11 32 views
1

我有兩個類使一個變量在地圖的關鍵

class Phone extends Observable 
    { 
    @observable String type = ''; 
    @observable String provider = ''; 
    @observable String num = ''; 

    Map<String, Map<String, String> map = {}; 

    Phone() {} 
    Phone.build({ this.type, 
        this.provider, 
        this.num }); 
    } 

我試圖使用字段值作爲關鍵的地圖,像這樣

Phone phone = new Phone(); 
    phone.map[phone.type] = {'type':'cell', 'provider':'Verizon', 'num':'1234567'}; 

,但它不工作。我怎樣才能使這些字段重視地圖的關鍵?

回答

2

因爲你正在使用中的字串例如,這可能並不適用,但要注意只是刪除引號

phone.map[phone.type] = {type:'cell', provider:'Verizon', num:'1234567'}; 

,如果您使用自定義類型爲Map鍵的情況下...

The keys of a `HashMap` must have consistent [Object.operator==] 
and [Object.hashCode] implementations. This means that the `==` operator 
must define a stable equivalence relation on the keys (reflexive, 
anti-symmetric, transitive, and consistent over time), and that `hashCode` 
must be the same for objects that are considered equal by `==`. 
相關問題