LinkedHashSet

LinkedHashSet具有可预知迭代顺序的Set接口的哈希表和链接列表实现。
<wbr style="line-height:25px">此实现与HashSet的不同之外在于,</wbr>LinkedHastSet维护着一个关于所有条目的双重链接列表。
<wbr style="line-height:25px"><wbr style="line-height:25px">此链接列表定义了迭代顺序,即按照将元素插入到集合中的顺序(插入顺序)进行迭代。</wbr></wbr>
<wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px">注意:</span>插入顺序不受在集合中重新插入的元素的影响。(<span style="color:#000080; line-height:25px">如果在s.contains(e)返回true后立即调用s.add(e),则该元素e 不会被重新插入到集合s中。</span>)<br style="line-height:25px"> 此实现可以让客户免遭未指定的、由HashSet提供的通常杂乱无章的排序工作,<br style="line-height:25px"> 而又不致引起与TreeSet关联的成本增加。使用它可以生成一个与原来顺序相同的集合副本,<br style="line-height:25px"> 并且与原集合的实现无关:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">voidfoo(Setm){<br style="line-height:25px"> Setcopy=newLinkedHashSet(m);<br style="line-height:25px"> ...<br style="line-height:25px"> }</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">如果模块通过输入得到一个集合,复制这个集合,然后返回由此副本决定了顺序的结果,<br style="line-height:25px"> 这种情况下这项技术特别有用。(客户通常期望内容返回的顺序与它们出现的顺序相同。)<br style="line-height:25px"> 此类提供所有可选的Set操作,并且允许null元素。与HashSet一样,它可以为基本操作(add、contains和remove)提供稳定的性能,<br style="line-height:25px"> 假定哈希函数将元素正确地分布到存储段中。由于增加了维护链接列表的开支,其性能很可能会比HashSet稍逊一筹,<br style="line-height:25px"> 不过,这一点例外:</span><span style="color:#000080; line-height:25px">LinkedHashSet迭代所需时间与集合的大小成正比,而与容量无关</span><span style="color:#003366; line-height:25px">。</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">HashSet迭代很可能支出较大,因为它所需迭代时间与其容量成正比。<br style="line-height:25px"> 链接的哈希集合有两个影响其性能的参数:</span><span style="color:#99cc00; line-height:25px">初始容量</span><span style="color:#000080; line-height:25px">和</span><span style="color:#808000; line-height:25px">加载因子</span><span style="color:#000080; line-height:25px">。它们与HashSet中的定义极其相同。</span><br style="line-height:25px"><span style="line-height:25px">注意:</span>为初始容量选择非常高的值对此类的影响比对HashSet要小,因为此类的迭代时间不受容量的影响。<br style="line-height:25px"><span style="line-height:25px">注意</span>:此实现不是同步的。如果多个线程同时访问链接的哈希集合,而其中至少一个线程修改了该集合,<br style="line-height:25px"> 则它必须保持外部同步。这一般通过对自然封装该集合的对象进行同步操作来完成。<br style="line-height:25px"> 如果不存在这样的对象,则应该使用Collections.synchronizedSet方法来“包装”该集合。<br style="line-height:25px"> 最好在创建时完成这一操作,以防止意外的非同步访问:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Sets=Collections.synchronizedSet(newLinkedHashSet(...));</span><br style="line-height:25px"> 此类的iterator方法返回的迭代器是快速失败的:在迭代器创建之后,如果对集合进行修改,<br style="line-height:25px"> 除非通过迭代器自身的移除方法,其他任何时间任何方式的修改,迭代器都将抛出ConcurrentModificationException。<br style="line-height:25px"> 因此,面对并发的修改,迭代器很快就会完全失败,而不冒将来不确定的时间任意发生不确定行为的风险。<br style="line-height:25px"> 注意,迭代器的快速失败行为不能得到保证,一般来说,存在不同步的并发修改时,不可能作出任何强有力的保证。<br style="line-height:25px"> 快速失败迭代器尽最大努力抛出ConcurrentModificationException。<br style="line-height:25px"> 因此,编写依赖于此异常的程序的方式是错误的,正确做法是:迭代器的快速失败行为应该仅用于检测程序错误。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:允许null元素,但最多只能加一个。如果LinkedHashSet已经有null元素,<br style="line-height:25px"> 试图再加null,不会成功,也不会抛异常。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:实现不同步的,不是线程安全的。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意3</wbr></span><wbr style="line-height:25px">:此类的iterator方法返回的迭代器是快速失败的:在创建迭代器之后,如果对集合进行修改,<br style="line-height:25px"> 除非通过迭代器自身的remove方法,否则在任何时间以任何方式对其进行修改,<br style="line-height:25px"> Iterator都将抛出ConcurrentModificationException<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意4</wbr></span><wbr style="line-height:25px">:iterator()返回的迭代器,里面的元素是插入的顺序排序的(先插入的在前面).<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意5</wbr></span><wbr style="line-height:25px">:关于对象的相等和HashSet一样。<br style="line-height:25px"> 具体可以查考HashSet和《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792010751168502/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">hashcode()和equals()及HashSet判断对象相等</a></strong></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr style="line-height:25px">注意6</wbr><wbr style="line-height:25px">:文档中说”为初始容量选择非常高的值对此类的影响比对HashSet要小“,应是指迭代时间的影响。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意7</wbr></span><wbr style="line-height:25px">:文档中以下描述<br style="line-height:25px"> ”(如果在s.contains(e)返回true后立即调用s.add(e),则元素e会被重新插入到集合s中。)“<br style="line-height:25px"><span style="color:#000080; line-height:25px"><span style="line-height:25px"></span><wbr style="line-height:25px">不正确<wbr style="line-height:25px">。<wbr style="line-height:25px">事实是:当试图添加一个重复元素到LinkedHashSet时,新元素并不会把旧元素替换掉,<br style="line-height:25px"> 而只是新元素不会添加到LinkedHashSet不会抛异常<wbr style="line-height:25px">。</wbr></wbr></wbr></wbr></span><br style="line-height:25px"> 当然如果插入重复元素,是以第一次插入的顺序为准。例1就是个很好的证明。<br style="line-height:25px"> 例1:<br style="line-height:25px"> i<span style="color:#3366ff; line-height:25px">mportjava.util.LinkedHashSet;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px">Test{</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px"> *@paramargs<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">main(</span><span style="color:#3366ff; line-height:25px">String[]args){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">LinkedHashSet&lt;People&gt;set=newLinkedHashSet();</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">set.add(newPeople("robin",1,21));<br style="line-height:25px"> set.add(newPeople("hb",2,20));<br style="line-height:25px"> set.add(newPeople("harry",9,30));<br style="line-height:25px"> set.add(null);<br style="line-height:25px"> Peoplep4=newPeople("robin",4,25);<br style="line-height:25px"> set.add(p4);<br style="line-height:25px"> set.add(newPeople("yp",5,28));<br style="line-height:25px"> set.add(newPeople("yp2",8,28));<br style="line-height:25px"> set.add(null);<br style="line-height:25px"> set.add(null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">for</span><span style="color:#3366ff; line-height:25px">(Peoplep:set)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">System.out.println(p);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">People{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringname;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intid;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intage;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">People(Stringname,intid)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this(name,id,0);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">People(Stringname,intid,intage)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.name=name;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.id=id;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.age=age;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">StringtoString()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnid+name+age;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicboolean</span><span style="color:#3366ff; line-height:25px">equals(Objecto)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(o==null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnfalse;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(!(oinstanceofPeople))</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnfalse;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Peoplep=(People)o;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">booleanres=name.equals(p.name);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(res)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("name"+name+"isdouble");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">else</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println(name+"vS"+p.name);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">res;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicint</span><span style="color:#3366ff; line-height:25px">hashCode()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnname.hashCode();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"></wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值