第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java中Map.containsKey的問題

Java中Map.containsKey的問題

一只萌萌小番薯 2019-03-21 18:19:12
我自己實現(xiàn)了一個類class Method{String className,methodName;Vector parameterTypes;}并把Method作為鍵值,寫了一個Map然后我在遍歷輸出這個Map的所有Key時候,能夠輸出我想要的那個對象a的內容,可是我寫Map.containsKey(a)的時候,它卻返回了false。我嘗試過自己改寫Object.equals方法,但還是沒用。改寫的equals方法如下:public boolean equals(Object anotherMethod){     if(!(anotherMethod instanceof Method))         return false;     Method another = (Method)anotherMethod;     if(! this.className.equals(another.className))         return false;     if(! this.methodName.equals(another.methodName))         return false;     if(this.parameterTypes == null && another.parameterTypes != null)         return false;     if(this.parameterTypes != null && another.parameterTypes == null)         return false;     if(this.parameterTypes.size() != another.parameterTypes.size())         return false;     for(int i=0; i<this.parameterTypes.size(); i++){         if(!this.parameterTypes.elementAt(i).equals(another.parameterTypes.elementAt(i)))             return false;     }     return true; }另外,我嘗試了如下代碼:methodMap.put(method, methodList.size());//methodList.size()是一個int System.out.println(methodMap.containsKey(method));//輸出true System.out.println(methodMap.containsKey(new Method(method)));//輸出false System.out.println(method.equals(new Method(method)));//輸出true請問這是什么原因,應該如何解決?
查看完整描述

1 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

解決了,用hashmap的時候應該重寫hashCode方法。


查看完整回答
反對 回復 2019-05-06
  • 1 回答
  • 0 關注
  • 693 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號