首页 > 其他 > 详细

关于Set-List-Map的比较学习

时间:2014-04-12 00:51:02      阅读:325      评论:0      收藏:0      [点我收藏+]

1、Set

The HashSet stores elements using a rather complex approach that will be explored in the Containers in Depth chapter—all you need to know at this point is that this technique is the fastest way to retrieveelements, and as a result the storage order can seem nonsensical (often, you only care whether something is a member of the Set, not the order in which it appears). If storage order is important, you can use a TreeSet, which keeps the objects in ascending comparison order, or a LinkedHashSetwhich keeps the objects in the order in which they were added.

(1)、HashSet:HashSet集合判断两个元素相等的标准是两个对象通过equals方法比较相等,并且两个对象的HashCode()方法返回值也相等——也就是说HashSet会在两个条件都满足的情况下才会判断两个元素相等

但在实际实现时应该注意:如果需要某个类的对象保存到HashSet集合中,重写这个类的equals()方法和HashCode()方法时,应该尽量保证两个对象通过equals()方法比较返回true时,他们的HashCode方法返回值也相等

 根据《Effective Java》第九条所说:“Always override hashCodewhen you override equals(覆盖equals总要覆盖hashCode)”;equal objects must have equal hash codes

Set的三个实现类HashSet、TreeSet和EnumSet都是线程不安全的

 

关于Set-List-Map的比较学习,布布扣,bubuko.com

关于Set-List-Map的比较学习

原文:http://www.cnblogs.com/CoolRandy/p/3658557.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!