忍者ブログ
よくわからないことを調べて解説してみるブログ。

2024

0420
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

2015

0524
  • あるオブジェクト同士のequalsメソッドがtrueを返すとき、それぞれのオブジェクトのhashCodeは同一の値を返却しなくてはならない。
  • ある2つのオブジェクトのhashCodeが不一致であるとき、そのオブジェクト同士によるequalsメソッドはfalseを返却しなくてはならない。
  • ある2つのオブジェクト同士のequalsメソッドがfalseを返すとき、それぞれのオブジェクトはtrue,falseのいずれの値を返却しても良い。
  • hashCodeは、同一のオブジェクトであるかぎり、同一の値を返さなくてはならない。
例1
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 = true
unit2 hash = 2, set contains unit2 = true
unit1 hash = 1, set contains unit1 = false

拍手[0回]

PR
Post your Comment
Name:
Title:
Font:
Mail:
URL:
Comment:
Pass: Vodafone絵文字 i-mode絵文字 Ezweb絵文字
カルティエ激安ブランド館
最高品質の商品を低価格のブラン ドコピー通販

ブラン ドコピー時計
ブラン ドコピー 服
ブラン ドコピー専門店
ブラン ドコピー財布
ブラン ドコピー 国内発送
ブランドブラン ドコピー専門店
ブランドブラン ドコピー 新作
ブランドブラン ドコピー 服
ブランドブラン ドコピー時計
ブランドブラン ドコピー靴
ブラン ド コピーN級
ブラン ド コピー代引き
ブラン ド コピー財布
サングラス ブラン ドコピー
キーケース ブラン ドコピー
コピーブラン ド 時計
コピーブラン ド通販
コピーブラン ド財布
ブランドコピー
ブランドコピーブラン ド

ホームページ上でのご注文は24時間受け付けております
カルティエ激安ブランド館 http://www.baggobuy.com/brandcopy-l-1.html
プロフィール
HN:
たんてーくん
性別:
非公開
フリーエリア
最新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]
ブログ内検索
忍者ブログ [PR]
* Template by TMP