在看老代码时,看到一处使用HashSet的场景,检查了放入HashSet的类型参数,发现这个类型并没有重写equals和hashCode方法,这个后果的严重程度可想而知。就此暂时总结了以下几点,并配合测试代码,共勉!
总结点如下:
1. 使用HashSet/HahsMap时,定义的元素/key的类型必须同时重写equals和hashCode方法。
TreeSet来说,只需实现Comparable接口,不需要重写equals和hashCode方法,至少java6是这样
对其他容器(无hash),建议都重写equals方法,否则无法支持查找和删除操作。比如使用PriorityQueue时,若仅实现Comparable只支持对象的插入,只有在自定义类实现了equals方法后,才支持查找、删除等操作了。
其中,最重要的就是第一条,需谨记。若不确定对象会被如何使用,建议对任何自定义类型重写equals、hashCode和toString方法。
测试代码中示例类型的说明:
BasicType
一个基础的类,包含三个字段,只重写了toString方法
ComparableType
一个可比较的类,只实现了Comparable接口
EqualsType
在BasicType的基础上重写了equals方法
HashCodeType
在BasicType的基础上重写了hashCode方法
EqualsComparableType
在ComparableType的基础上重写了equals方法
HashType
在EqualsType基础上重写了hashCode方法
设计的类图结构:
测试用例
对HashSet的测试
使用BasicType对HashSet进行测试
/**
* 使用{@link BasicType}对HashSet进行测试,对不同对象,equals为false,hashCode不相等
* 因此任一{@link BasicType}的对象在HashSet中都是唯一的,见测试{@link #testBasicTypeInHashSet}
*
* @see BasicType
* @see test.HashTypeTest#testHashTypeInHashSet testHashTypeInHashSet
*/
@Test
public void testBasicTypeInHashSet() {
Set set = new HashSet();
set.add(new BasicType());
set.add(new BasicType());
set.add(new BasicType());
set.add(new BasicType());
BasicType t = new BasicType();
set.add(t);
set.add(t);
// size为5,不是我们想要的
Assert.assertEquals(set.size(), 5);
}
使用EqualsType对HashSet进行测试
/**
* 使用{@link EqualsType}对HashSet进行测试,对不同对象,equals可能为true,hashCode不相等
* 任一{@link EqualsType}的对象在HashSet中都是唯一的
*
* @see EqualsType
* @see test.HashTypeTest#testHashTypeInHashSet testHashTypeInHashSet
*/
@Test
public void testEqualsTypeInHashSet() {
Set set = new HashSet();
set.add(new EqualsType());
set.add(new EqualsType());
set.add(new EqualsType());
set.add(new EqualsType());
EqualsType t = new EqualsType();
set.add(t);
set.add(t);
// size为5,不是我们想要的
Assert.assertEquals(set.size(), 5);
}
使用HashCodeType对HashSet进行测试
/**
* 使用{@link HashCodeType}对HashSet进行测试,对不同对象,equals可能为false,hashCode可能相等
* 任一{@link HashCodeType}的对象在HashSet中都是唯一的
*
* @see HashCodeType
* @see test.HashTypeTest#testHashTypeInHashSet testHashTypeInHashSet
*/
@Test
public void testHashCodeTypeInHashSet() {
Set set = new HashSet();
set.add(new HashCodeType());
set.add(new HashCodeType());
set.add(new HashCodeType());
set.add(new HashCodeType());
HashCodeType t = new HashCodeType();
set.add(t);
set.add(t);
// size为5,不是我们想要的
Assert.assertEquals(set.size(), 5);
}
使用HashType对HashSet进行测试
/**
* 使用{@link HashType}对HashSet进行测试,对不同对象,equals可能为true,hashCode可能相等
* 此时可以用HashSet去除重复对象(hashCode相等且是equals的),正是我们想要的
*
* @see HashType
*/
@Test
public void testHashTypeInHashSet() {
Set set = new HashSet();
set.add(new HashType());
set.add(new HashType());
set.add(new HashType());
set.add(new HashType());
HashType t = new HashType();
set.add(t);
set.add(t);
// 相等值对象被去除,size为1, 正是我们想要的
Assert.assertEquals(set.size(), 1);
}
对TreeSet的测试
使用ComparableType对TreeSet进行测试
/**
* 使用ComparableType对TreeSet进行测试
* 使用TreeSet的类必须实现Comparable接口,但不必重写equals和hashCode方法,与TreeSet的内部实现有关
*
* @see tijava.container.type.ComparableType
*/
@Test
public void testComparableTypeInTreeSet() {
Set q = new TreeSet();
q.add(new ComparableType(‘d’, 3, null));
q.add(new ComparableType(‘d’, 4, null));
q.add(new ComparableType());
q.add(new ComparableType());
q.add(new ComparableType());
q.add(new ComparableType());
q.add(new ComparableType());
Assert.assertEquals(q.size(), 3);
Assert.assertTrue(q.contains(new ComparableType('d', 3, null)));
q.remove(new ComparableType('d', 3, null));
//remove ok
Assert.assertEquals(q.size(), 2);
}
对PriorityQueue的测试
使用ComparableType对PriorityQueue进行测试
/**
* 使用ComparableType对PriorityQueue进行测试
* 在PriorityQueue中使用自定义类时,若只实现Comparable接口的类型,不支持查找和删除等操作
*
* @see test.EqualsComparableTypeTest#testEqualsComparableTypeInPriorityQueue
* testEqualsComparableTypeInPriorityQueue
*/
@Test
public void testComparableTypeInPriorityQueue() {
Queue q = new PriorityQueue();
q.add(new ComparableType(‘C’, 4, “Empty trash”));
q.add(new ComparableType(‘A’, 2, “Feed dog”));
q.add(new ComparableType(‘B’, 7, “Feed bird”));
q.add(new ComparableType(‘C’, 3, “Mow lawn”));
q.add(new ComparableType(‘A’, 1, “Water lawn”));
q.add(new ComparableType(‘B’, 1, “Feed cat”));
Assert.assertEquals(q.size(), 6);
Assert.assertFalse(q
.contains(new ComparableType('C', 4, "Empty trash")));
Assert.assertFalse(q.remove(new ComparableType('C', 4, "Empty trash")));
//siz is still 6, not remove success
Assert.assertEquals(q.size(), 6);
}
使用EqualsComparableType对PriorityQueue进行测试
/**
* 使用EqualsComparableType对PriorityQueue进行测试
* 在使用PriorityQueue是,在自定义类实现了equals方法后,就支持查找、删除等操作了
*
* @see test.ComparableTypeTest#testComparableTypeInPriorityQueue
* testComparableTypeInPriorityQueue
*/
@Test
public void testEqualsComparableTypeInPriorityQueue() {
Queue q = new PriorityQueue();
q.add(new EqualsComparableType(‘C’, 4, “Empty trash”));
q.add(new EqualsComparableType(‘A’, 2, “Feed dog”));
q.add(new EqualsComparableType(‘B’, 7, “Feed bird”));
q.add(new EqualsComparableType(‘C’, 3, “Mow lawn”));
q.add(new EqualsComparableType(‘A’, 1, “Water lawn”));
q.add(new EqualsComparableType(‘B’, 1, “Feed cat”));
Assert.assertEquals(q.size(), 6);
Assert.assertTrue(q.contains(new EqualsComparableType('C', 4,
"Empty trash")));
Assert.assertTrue(q.remove(new EqualsComparableType('C', 4,
"Empty trash")));
// remove ok
Assert.assertEquals(q.size(), 5);
}