IdentityHashMap

java.util.IdentityHashMap 类利用哈希表实现Map接口,比较键(和值)时使用<wbr style="line-height:25px"><span style="color:#003366; line-height:25px">引用相等性代替对象相等性</span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">换句话说,在IdentityHashMap中,当且仅当</span><span style="color:#ff6600; line-height:25px">(k1==k2</span><span style="color:#003366; line-height:25px">)时,才认为</span><span style="color:#0000ff; line-height:25px">两个键k1和k2相等</span><span style="color:#003366; line-height:25px">(在正常Map实现(如HashMap)中,<br style="line-height:25px"> 当且仅当满足下列条件时才认为两个键k1和k2相等:(</span><span style="color:#0000ff; line-height:25px">k1==null?k2==null:e1.equals(e2))</span><span style="color:#003366; line-height:25px">)。</span><br style="line-height:25px"> 此类不是通用Map实现!此类实现Map接口时,它有意违反Map的常规协定,<br style="line-height:25px"><span style="color:#000080; line-height:25px">该协定在比较对象时强制使用equals方法。此类设计仅用于其中需要引用相等性语义的罕见情况。此类的典型用法是拓扑保留对象图形转换,如序列化或深层复制。</span><br style="line-height:25px"> 要执行这样的转换,程序必须维护用于跟踪所有已处理对象引用的“节点表”。<br style="line-height:25px"> 节点表一定不等于不同对象,即使它们偶然相等也如此。此类的另一种典型用法是维护代理对象。<br style="line-height:25px"> 例如,调试设施可能希望为正在调试程序中的每个对象维护代理对象。<br style="line-height:25px"> 此类提供所有的可选映射操作,并且允许null值和null键。此类对映射的顺序不提供任何保证;特别是不保证顺序随时间的推移保持不变。<br style="line-height:25px"><span style="color:#000080; line-height:25px">此类提供基本操作(get和put)的稳定性能,假定系统标识了将桶间元素正确分开的哈希函数(System.identityHashCode(Object))。<br style="line-height:25px"> 此类具有一个调整参数(影响性能但不影响语义):expectedmaximumsize。此参数是希望映射保持的键值映射关系最大数。<br style="line-height:25px"> 在内部,此参数用于确定最初组成哈希表的桶数。未指定所期望的最大数量和桶数之间的确切关系。</span> <div style="line-height:25px"> <span style="color:#000080; line-height:25px">默认的价值加载因子为</span><span style="color:#ff9900; line-height:25px">2/3</span><span style="color:#000080; line-height:25px">,在重新哈希后,加载因子变为</span><span style="color:#ff9900; line-height:25px">1/3</span><span style="color:#000080; line-height:25px">.当哈希表中的条目数超出了加载因子与当前容量的乘积时,通过调用 reszie 方法将</span><span style="color:#0000ff; line-height:25px">容量翻倍</span><span style="color:#000080; line-height:25px">,重新进行哈希。</span><span style="color:#000080; line-height:25px">增加桶数,重新哈希,可能相当昂贵。<br style="line-height:25px"> 因此创建具有足够大的期望最大数量的标识哈希映射更合算。另一方面,对collection视图进行迭代所需的时间与哈希表中的桶数成正比,<br style="line-height:25px"> 所以如果特别注重迭代性能或内存使用,则不宜将期望的最大数量设置得过高。</span><br style="line-height:25px"> 注意,此实现不是同步的。如果多个线程同时访问此映射,并且其中至少一个线程从结构上修改了该映射,<br style="line-height:25px"> 则其必须保持外部同步(结构上的修改是指添加或删除一个或多个映射关系的操作;仅改变与实例已经包含的键关联的值不是结构上的修改。)<br style="line-height:25px"> 这一般通过对自然封装该映射的对象进行同步操作来完成。如果不存在这样的对象,则应该使用Collections.synchronizedMap方法来“包装”该映射。<br style="line-height:25px"> 最好在创建时完成这一操作,以防止对映射进行意外的不同步访问,如下所示:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Mapm=Collections.synchronizedMap(newHashMap(...));</span><br style="line-height:25px"> 由所有此类的“collection视图方法”所返回的迭代器都是快速失败的:在迭代器创建之后,<br style="line-height:25px"> 如果从结构上对映射进行修改,除非通过迭代器自身的remove或add方法,其他任何时间任何方式的修改,<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="color:#000080; line-height:25px">此为简单的线性探头哈希表,如Sedgewick和Knuth原文示例中所述。<br style="line-height:25px"> 该数组交替保持键和值(对于大型表来说,它比使用独立组保持键和值更具优势)。对于多数JRE实现和混合操作,<br style="line-height:25px"> 此类比HashMap(它使用链而不使用线性探头)能产生更好的性能。</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:允许null值和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">:此实现不是同步的。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意4</wbr></span><wbr style="line-height:25px">:迭代器的快速失败行为不能得到保证。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意5</wbr></span><wbr style="line-height:25px">:<span style="color:#000080; line-height:25px">此为简单的线性探头哈希表。对于多数JRE实现和混合操作,<br style="line-height:25px"> 此类比HashMap(它使用链而不使用线性探头)能产生更好的性能。</span><br style="line-height:25px"><div style="line-height:25px"><span style="line-height:25px">构造函数</span></div> <div style="line-height:25px"> <span style="line-height:normal; color:rgb(51,51,51); font-family:arial,sans-serif; font-size:13px"></span> <table id="pubctors" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:0.9em; border-collapse:collapse; empty-cells:show; width:933px"><tbody style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <th colspan="12" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:rgb(222,232,241)"> Public Constructors</th> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"></nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/IdentityHashMap.html#IdentityHashMap()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">IdentityHashMap</a></span>()</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Creates an IdentityHashMap with default expected maximum size.</div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"></nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/IdentityHashMap.html#IdentityHashMap(int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">IdentityHashMap</a></span>(int maxSize)</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Creates an IdentityHashMap with the specified maximum size parameter.</div> </td> </tr> <tr style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; background-color:rgb(246,246,246)"> <td style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"></nobr> </td> <td width="100%" style="line-height:21px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:6px; padding-right:12px; padding-bottom:6px; padding-left:12px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); text-align:left; vertical-align:top; background-color:inherit"> <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/IdentityHashMap.html#IdentityHashMap(java.util.Map&lt;?%20extends%20K,%20?%20extends%20V&gt;)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">IdentityHashMap</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/util/Map.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Map</a>&lt;?extendsK,?extendsV&gt; map)</nobr><div style="line-height:25px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:3px; padding-right:1em; padding-bottom:0px; padding-left:1em; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; border-style:initial; border-color:initial"> Creates an IdentityHashMap using the given map as initial values.</div> </td> </tr> </tbody></table> </div> <div style="line-height:25px">结束!</div> <br style="line-height:25px"></wbr></wbr></wbr></wbr></wbr> </div> </wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值