关于Java中使用容器的几个注意点
来源:互联网 发布:贵州网络微商传销 编辑:程序博客网 时间:2024/04/29 07:16
关于Java中使用容器的几个注意点
在看老代码时,看到一处使用HashSet的场景,检查了放入HashSet的类型参数,发现这个类型并没有重写equals和hashCode方法,这个后果的严重程度可想而知。就此暂时总结了以下几点,并配合测试代码,共勉!
总结点如下:
1. 使用HashSet/HahsMap时,定义的元素/key的类型必须同时重写equals和hashCode方法。
2. TreeSet来说,只需实现Comparable接口,不需要重写equals和hashCode方法,至少java6是这样
3. 对其他容器(无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不相等<br> * 因此任一{@link BasicType}的对象在HashSet中都是唯一的,见测试{@link #testBasicTypeInHashSet} * * @see BasicType * @see test.HashTypeTest#testHashTypeInHashSet testHashTypeInHashSet */@Testpublic void testBasicTypeInHashSet() {Set<BasicType> set = new HashSet<BasicType>();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不相等<br> * 任一{@link EqualsType}的对象在HashSet中都是唯一的 * * @see EqualsType * @see test.HashTypeTest#testHashTypeInHashSet testHashTypeInHashSet */@Testpublic void testEqualsTypeInHashSet() {Set<EqualsType> set = new HashSet<EqualsType>();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可能相等<br> * 任一{@link HashCodeType}的对象在HashSet中都是唯一的 * * @see HashCodeType * @see test.HashTypeTest#testHashTypeInHashSet testHashTypeInHashSet */@Testpublic void testHashCodeTypeInHashSet() {Set<HashCodeType> set = new HashSet<HashCodeType>();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可能相等<br> * 此时可以用HashSet去除重复对象(hashCode相等且是equals的),正是我们想要的 * * @see HashType */@Testpublic void testHashTypeInHashSet() {Set<HashType> set = new HashSet<HashType>();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进行测试<br> * 使用TreeSet的类必须实现Comparable接口,但不必重写equals和hashCode方法,与TreeSet的内部实现有关 * * @see tijava.container.type.ComparableType */@Testpublic void testComparableTypeInTreeSet() {Set<ComparableType> q = new TreeSet<ComparableType>();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 okAssert.assertEquals(q.size(), 2);}
对PriorityQueue的测试
使用ComparableType对PriorityQueue进行测试
/** * 使用ComparableType对PriorityQueue进行测试<br> * 在PriorityQueue中使用自定义类时,若只实现Comparable接口的类型,不支持查找和删除等操作 * * @see test.EqualsComparableTypeTest#testEqualsComparableTypeInPriorityQueue * testEqualsComparableTypeInPriorityQueue */@Testpublic void testComparableTypeInPriorityQueue() {Queue<ComparableType> q = new PriorityQueue<ComparableType>();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 successAssert.assertEquals(q.size(), 6);}
使用EqualsComparableType对PriorityQueue进行测试
/** * 使用EqualsComparableType对PriorityQueue进行测试<br> * 在使用PriorityQueue是,在自定义类实现了equals方法后,就支持查找、删除等操作了 * * @see test.ComparableTypeTest#testComparableTypeInPriorityQueue * testComparableTypeInPriorityQueue */@Testpublic void testEqualsComparableTypeInPriorityQueue() {Queue<EqualsComparableType> q = new PriorityQueue<EqualsComparableType>();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 okAssert.assertEquals(q.size(), 5);}
- 关于Java中使用容器的几个注意点
- 关于Servlet中Path的几个注意点
- 关于JAVA中"+"的注意点
- java中方法override和overload的几个注意点
- Java中面向对象的几个注意点
- 关于addSubView需要注意的几个点
- 关于SQL优化的几个注意点
- 关于addSubView需要注意的几个点
- 关于addSubView需要注意的几个点
- 关于重定向的几个注意点
- 关于抽象类的几个注意点
- c++中几个需要注意的点
- 使用lucene的几个注意点
- abator使用后的几个注意点
- DLL使用的几个注意点
- 使用模板库的几个注意点
- CEF使用的几个注意点
- 动态库使用的几个注意点
- Html Agility Pack学习(一):HtmlAgilityPack类的简单应用
- 链表程序引发的指针思考
- wxPython in Action (EVT_CLOSE)当关闭一个框架时都发生了什么?
- RAID--磁盘阵列概念详解 - [磁盘阵列]
- wxPython frame的布局详细解释
- 关于Java中使用容器的几个注意点
- wx.Frame
- wxPython in Action (MDIParentFrame
- wxPython in Action (MessageDialog)
- 启动虚拟化支持后,网卡无法激活
- wxPython in Action (TextEntryDialog
- wxPython in Action (ProgressDialog)
- PyGridTableBase的方法
- wxPython in Action (wx.Timer)