よくわからないことを調べて解説してみるブログ。
2015
- あるオブジェクト同士のequalsメソッドがtrueを返すとき、それぞれのオブジェクトのhashCodeは同一の値を返却しなくてはならない。
- ある2つのオブジェクトのhashCodeが不一致であるとき、そのオブジェクト同士によるequalsメソッドはfalseを返却しなくてはならない。
- ある2つのオブジェクト同士のequalsメソッドがfalseを返すとき、それぞれのオブジェクトはtrue,falseのいずれの値を返却しても良い。
- hashCodeは、同一のオブジェクトであるかぎり、同一の値を返さなくてはならない。
import java.util.HashSet;
public class EqualsHashCodeError1 {
public static void main(String[] args) {
Unit unit1 = new EqualsHashCodeError1().new Unit(1);
Unit unit2 = new EqualsHashCodeError1().new Unit(2);
HashSet set = new HashSet();
set.add(unit1);
set.add(unit2);
System.out.println("unit1 hash = " + unit1.hashCode() + ", set contains unit1 = " + set.contains(unit1));
System.out.println("unit2 hash = " + unit2.hashCode() + ", set contains unit2 = " + set.contains(unit2));
}
class Unit {
int id;
public Unit(int id) {
this.id = id;
}
@Override
public int hashCode() {
return id++;// hashCodeはいかなる状況でも同じ値を返すこと。
}
@Override
public boolean equals(Object obj) {
return this.id == ((Unit) obj).id;
}
}
}
実行結果
unit1 hash = 2, set contains unit1 = false
unit2 hash = 3, set contains unit2 = false
例2
import java.util.Hashtable;
public class EqualsHashCodeError2 {
public static void main(String[] args) {
Unit unit1 = new EqualsHashCodeError2().new Unit(1);
Unit unit2 = new EqualsHashCodeError2().new Unit(2);
Hashtable<Integer, Unit> map = new Hashtable<Integer, EqualsHashCodeError2.Unit>();
for (int i = 0; i < 1000; i++) {
map.put(i, new EqualsHashCodeError2().new Unit(i));
}
System.out.println("unit1 hash = " + unit1.hashCode() + ", set contains unit1 = " + map.containsValue(unit1));
System.out.println("unit2 hash = " + unit2.hashCode() + ", set contains unit2 = " + map.containsValue(unit2));
System.out.println("unit1 hash = " + unit1.hashCode() + ", set contains unit1 = " + map.containsValue(unit1));
}
class Unit {
int id;
boolean equals;
public Unit(int id) {
this.id = id;
}
@Override
public int hashCode() {
return id;// hashCodeはいかなる状況でも同じ値を返すこと。
}
@Override
public boolean equals(Object obj) {
if (this.id == ((Unit) obj).id) {
this.id = (int) Math.random() * 100;
return true; // equalsがtrueを返すとき、hashCodeは同じ値となる。
}
return false;
}
}
}
実行結果
unit1 hash = 1, set contains unit1 = trueunit2 hash = 2, set contains unit2 = trueunit1 hash = 1, set contains unit1 = false
PR
Post your Comment
カルティエ激安ブランド館
最高品質の商品を低価格のブラン ドコピー通販
ブラン ドコピー時計
ブラン ドコピー 服
ブラン ドコピー専門店
ブラン ドコピー財布
ブラン ドコピー 国内発送
ブランドブラン ドコピー専門店
ブランドブラン ドコピー 新作
ブランドブラン ドコピー 服
ブランドブラン ドコピー時計
ブランドブラン ドコピー靴
ブラン ド コピーN級
ブラン ド コピー代引き
ブラン ド コピー財布
サングラス ブラン ドコピー
キーケース ブラン ドコピー
コピーブラン ド 時計
コピーブラン ド通販
コピーブラン ド財布
ブランドコピー
ブランドコピーブラン ド
ホームページ上でのご注文は24時間受け付けております
カルティエ激安ブランド館 http://www.baggobuy.com/brandcopy-l-1.html
ブラン ドコピー時計
ブラン ドコピー 服
ブラン ドコピー専門店
ブラン ドコピー財布
ブラン ドコピー 国内発送
ブランドブラン ドコピー専門店
ブランドブラン ドコピー 新作
ブランドブラン ドコピー 服
ブランドブラン ドコピー時計
ブランドブラン ドコピー靴
ブラン ド コピーN級
ブラン ド コピー代引き
ブラン ド コピー財布
サングラス ブラン ドコピー
キーケース ブラン ドコピー
コピーブラン ド 時計
コピーブラン ド通販
コピーブラン ド財布
ブランドコピー
ブランドコピーブラン ド
ホームページ上でのご注文は24時間受け付けております
カルティエ激安ブランド館 http://www.baggobuy.com/brandcopy-l-1.html
プロフィール
HN:
たんてーくん
性別:
非公開
最新記事
(03/29)
(03/29)
(06/26)
(05/21)
(04/23)
カテゴリー
最新CM
[09/25 http://2017.bblbuy.com]
[09/24 http://www.japanform.com]
[09/23 http://www.japanform.com]
[09/22 http://www.japanform.com]
[09/21 http://2017.bblbuy.com]
ブログ内検索
