当Set元素改变时的行为

先看这段程序:

import java.util.*;

class KeyMaster {
	public int i;

	public KeyMaster(int i) {
		this.i = i;
	}

	public boolean equals(Object o) {
		return i == ((KeyMaster) o).i;
	}

	public int hashCode() {
		return i;
	}
}

public class Test {
	public static void main(String[] args) {
		Set<KeyMaster> set = new HashSet<KeyMaster>();
		KeyMaster k1 = new KeyMaster(1);
		KeyMaster k2 = new KeyMaster(2);
		set.add(k1);
		set.add(k1);
		set.add(k2);
		set.add(k2);
		System.out.print(set.size() + ":");
		k2.i = 1;
		System.out.print(set.size() + ":");
		set.remove(k1);
		System.out.print(set.size() + ":");
		set.remove(k2);
		System.out.print(set.size());
	}
}

 

 

 

 

 

 

 

结果是什么呢?

2:2:1:1

也就是说添加的时候根据equals方法判断,只进去了两个不同i值的对象,但删除时为什么第二个删除不了呢?发现如果去掉override的hashCode方法时结果为2:2:1:0

文档中有这样一段:

Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.

说有可变元素时行为未定,但remove方法文档中也没提到可能会被hashCode影响到?

 

2009.08.27回答:

remove之前可能是要先对比hashCode的,而k2的hashCode改变之后导致无法找到这个元素,导致remove失败

你可能感兴趣的:(set)