LinkedHashMap

本文详细介绍了Java中LinkedHashMap的实现原理及其特性,包括迭代顺序、访问顺序、插入顺序等概念,并探讨了如何通过重写removeEldestEntry方法实现LRU缓存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

publicclassLinkedHashMap<K,V>
extendsHashMap<K,V>
implementsMap<K,V>
<wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px">LinkedHashMap</span><span style="color:#003366; line-height:25px">是Map接口的哈希表和链接列表实现,具有可预知的迭代顺序。<br style="line-height:25px"> 此实现与HashMap的不同之处在于,后者维护着一个运行于所有条目的双重链接列表。</span><wbr style="line-height:25px"><br style="line-height:25px"> 此链接列表定义了迭代顺序,该迭代顺序通常就是将键插入到映射中的顺序(插入顺序)。<br style="line-height:25px"> 注意,如果在映射中重新插入键,则插入顺序不受影响。<br style="line-height:25px"> (如果在调用m.put(k,v)前m.containsKey(k)返回了true,则调用时会将键k重新插入到映射m中。)<br style="line-height:25px"> 此实现可以让客户避免未指定的、由HashMap(及Hashtable)所提供的通常为杂乱无章的排序工作,<br style="line-height:25px"> 同时无需增加与TreeMap相关的成本。使用它可以生成一个与原来顺序相同的映射副本,而与原映射的实现无关:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">voidfoo(Mapm){<br style="line-height:25px"> Mapcopy=newLinkedHashMap(m);<br style="line-height:25px"> ...<br style="line-height:25px"> }</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">如果模块通过输入得到一个映射,复制这个映射,<br style="line-height:25px"> 然后返回由此副本确定其顺序的结果,这种情况下这项技术特别有用。(客户通常期望返回的内容与其出现的顺序相同。)<br style="line-height:25px"> 提供特殊的构造方法来创建链接哈希映射,该哈希映射的迭代顺序就是最后访问其条目的顺序,<br style="line-height:25px"> 从近期访问最少到近期访问最多的顺序(访问顺序)。</span><span style="color:#ff9900; line-height:25px">这种映射很适合构建LRU缓存。</span><br style="line-height:25px"> 调用put或get方法将会访问相应的条目(假定调用完成后它还存在)。<br style="line-height:25px"> putAll方法以指定映射的条目集合迭代器提供的键-值映射关系的顺序,为指定映射的每个映射关系生成一个条目访问。<br style="line-height:25px"> 任何其他方法均不生成条目访问。特别是,collection视图上的操作不影响底层映射的迭代顺序。<span style="color:#000080; line-height:25px"><br style="line-height:25px"></span>此类提供所有可选的Map操作,并且允许null元素。与HashMap一样,它可以为基本操作(add、contains和remove)提供稳定的性能,<br style="line-height:25px"> 假定哈希函数将元素正确分布到桶中。由于增加了维护链接列表的开支,其性能很可能比HashMap稍逊一筹,不过这一点例外:<br style="line-height:25px"> LinkedHashMap的collection视图迭代所需时间与映射的大小成比例。HashMap迭代时间很可能开支较大,因为它所需要的时间与其容量成比例。<br style="line-height:25px"> 链接的哈希映射具有两个影响其性能的参数:初始容量和加载因子。它们的定义与HashMap极其相似。<br style="line-height:25px"> 要注意,为初始容量选择非常高的值对此类的影响比对HashMap要小,因为此类的迭代时间不受容量的影响。<br style="line-height:25px"> 注意,<span style="line-height:25px"><wbr style="line-height:25px">此实现不是同步的</wbr></span><wbr 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(newLinkedHashMap(...));</span><br style="line-height:25px"> 结构修改是指添加或删除一个或多个映射关系,或者在按访问顺序链接的哈希映射中影响迭代顺序的任何操作。<br style="line-height:25px"> 在按插入顺序链接的哈希映射中,仅更改与映射中已包含键关联的值不是结构修改。<br style="line-height:25px"> 在按访问顺序链接的哈希映射中,仅利用get查询映射不是结构修改。)<br style="line-height:25px"> Collection(由此类的所有collection视图方法所返回)的iterator方法返回的迭代器都是快速失败的:<br style="line-height:25px"> 在迭代器创建之后,如果从结构上对映射进行修改,除非通过迭代器自身的移除方法,其他任何时间任何方式的修改,<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">:该实现不是同步的,不是线程安全的。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:关于哈希映射的概念初始容量和加载因子可参阅《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920107624957373/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">HashMap</a></strong></wbr></wbr></wbr></wbr></wbr>

<wbr style="line-height:25px">注意3</wbr><wbr style="line-height:25px">:<span style="color:#993300; line-height:25px">public</span><span style="color:#0000ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">LinkedHashMap</span><span style="color:#0000ff; line-height:25px">(</span><span style="color:#993300; line-height:25px">int</span><span style="color:#0000ff; line-height:25px">initialCapacity,</span><span style="color:#993300; line-height:25px">float</span><span style="color:#0000ff; line-height:25px">loadFactor,</span><span style="color:#993300; line-height:25px">boolean</span><span style="color:#0000ff; line-height:25px">accessOrder)</span><br style="line-height:25px"> 构造一个带指定初始容量、加载因子和排序模式的空LinkedHashMap实例。<br style="line-height:25px"> 参数:<br style="line-height:25px"> initialCapacity-初始容量。<br style="line-height:25px"> loadFactor-加载因子。<br style="line-height:25px"> accessOrder-排序模式-对于访问顺序,为true;对于插入顺序,则为false。<br style="line-height:25px"> 这里的“访问”包含了get(),put().<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"> 对于按照插入顺序(accessOrder为false)来排序的,是以前面一个插入顺序为准。<br style="line-height:25px"> 可以参照实例2。<br style="line-height:25px"><span style="line-height:25px; color:rgb(0,0,128)"><wbr style="line-height:22px">可以重写removeEldestEntry(Map.Entry)方法,<wbr style="line-height:22px">以便在将新映射关系添加到映射时自动移除旧的映射关系<wbr style="line-height:22px">。</wbr></wbr></wbr></span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">protectedbooleanremoveEldestEntry(Map.Entry&lt;K,V&gt;eldest)</span><br style="line-height:25px"> 此方法用以提供在每次添加新条目时移除最旧条目的策略。如果需要删除此时最旧的条目,则返回true。在将新条目插入到映射后, put和putAll将调用此方法。如果映射表示缓存,则此方法非常有用:<br style="line-height:25px"> 它允许映射通过删除旧条目来减少内存损耗。<br style="line-height:25px"> 示例用法:此重写允许映射增加到100个条目,然后每次添加新条目时删除最旧的条目,<br style="line-height:25px"> 始终维持100个条目的稳定状态。<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">privatestaticfinalintMAX_ENTRIES=100;<br style="line-height:25px"> protectedbooleanremoveEldestEntry(Map.Entryeldest){<br style="line-height:25px"> returnsize()&gt;MAX_ENTRIES;<br style="line-height:25px"> }</span><br style="line-height:25px"> 此方法通常不以任何方式修改映射,相反允许映射在其返回值的指引下进行自我修改。<br style="line-height:25px"> 使用此方法直接修改映射是允许的,但是如果它执行了此操作,则一定返回false(表示该映射不应进行任何进一步的修改)。<br style="line-height:25px"> 在此方法中修改映射后是否返回true是不确定的。<br style="line-height:25px"> 此实现仅返回false(这样,此映射的行为将类似于正常映射,即永远不能移除最旧的元素)。<br style="line-height:25px"><span style="color:#ff9900; line-height:25px"></span><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">removeEldestEntry</span><span style="color:#000080; line-height:25px">的目的主要是提供接口。</span><br style="line-height:25px"> 在JDK中1.6中的源码如下<wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">protectedboolean</span><span style="color:#3366ff; line-height:25px">removeEldestEntry(Map.Entry&lt;K,V&gt;eldest){<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">false;<br style="line-height:25px"> }<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">void</span><span style="color:#3366ff; line-height:25px">addEntry(inthash,Kkey,Vvalue,intbucketIndex){<br style="line-height:25px"> createEntry(hash,key,value,bucketIndex);<br style="line-height:25px"></span><span style="color:#808080; line-height:25px">//Removeeldestentryifinstructed,elsegrowcapacityifappropriate</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Entry&lt;K,V&gt;eldest=header.after;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span>if<span style="color:#3366ff; line-height:25px">(removeEldestEntry(eldest)){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">removeEntryForKey(eldest.key);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">else</span><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">if</span><span style="color:#3366ff; line-height:25px">(size&gt;=threshold)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">resize(2*table.length);</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="line-height:25px"><wbr style="line-height:25px">实例1</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.LinkedHashMap;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.Map;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.Random;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.concurrent.ConcurrentLinkedQueue;</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">ConcurrentLinkedQueue&lt;Integer&gt;queue=newConcurrentLinkedQueue&lt;Integer&gt;();</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//TODOAuto-generatedmethodstub</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">(inti=0;i&lt;1;i++)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newThread(newThreadProducer(queue)).start();</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">(inti=0;i&lt;1;i++)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newThread(newThreadConsumer(queue)).start();</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"></span><span style="color:#ff6600; line-height:25px">CacheLRU&lt;K,V&gt;</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">LinkedHashMap&lt;K,V&gt;</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">privatestaticfinalintMAX_ENTRIES=800;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">protectedboolean</span><span style="color:#3366ff; line-height:25px">removeEldestEntry(Map.Entry&lt;K,V&gt;eldest){</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">size()&gt;MAX_ENTRIES;</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">Image</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">byte[]data;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Image(byteindexData[])</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">data=newbyte[indexData.length*4];</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">ThreadProducerimplementsRunnable</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">ThreadProducer(ConcurrentLinkedQueue&lt;Integer&gt;queue)</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.queue=queue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">blRun=true;</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">ConcurrentLinkedQueue&lt;Integer&gt;queue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">staticintcnt=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">staticbooleanblRun=true;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">run()</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">intval=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Randomrandom=newRandom(System.currentTimeMillis());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">while(blRun)</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">cnt=(cnt+1)&amp;0xFFFFFFFF;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">try{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">val=random.nextInt()%1000;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(val&lt;0)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">val=-val;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(cnt&gt;10000)</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">val=-1;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">blRun=false;</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">queue.add(newInteger(val));</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread.sleep(1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}catch(InterruptedExceptione)</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">e.printStackTrace();</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:#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">ThreadConsumerimplementsRunnable<br style="line-height:25px"> {<br style="line-height:25px"> LinkedHashMap&lt;Integer,Image&gt;cache=newCacheLRU&lt;Integer,Image&gt;();<br style="line-height:25px"> ConcurrentLinkedQueue&lt;Integer&gt;queue;<br style="line-height:25px"> staticintcacheSuccessCount=0;<br style="line-height:25px"> staticintallCount=0;<br style="line-height:25px"> staticbooleanblRun=true;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">ThreadConsumer</span><span style="color:#3366ff; line-height:25px">(ConcurrentLinkedQueue&lt;Integer&gt;queue)<br style="line-height:25px"> {<br style="line-height:25px"> this.queue=queue;<br style="line-height:25px"> blRun=true;<br style="line-height:25px"> }<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">run()<br style="line-height:25px"> {<br style="line-height:25px"> Integerkey;<br style="line-height:25px"> Imageimg;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">(blRun)<br style="line-height:25px"> {<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"> if(!queue.isEmpty())<br style="line-height:25px"> {<br style="line-height:25px"> key=queue.poll();<br style="line-height:25px"> if(key.intValue()==-1)<br style="line-height:25px"> {<br style="line-height:25px"> blRun=false;<br style="line-height:25px"> break;<br style="line-height:25px"> }<br style="line-height:25px"> System.out.println("readimage"+key);<br style="line-height:25px"> img=cache.get(key);<br style="line-height:25px"> if(img==null)<br style="line-height:25px"> {<br style="line-height:25px"> byteimgIndexData[]=newbyte[10];<br style="line-height:25px"> img=newImage(imgIndexData);<br style="line-height:25px"> System.out.println("loadimage");<br style="line-height:25px"> cache.put(key,img);<br style="line-height:25px"> }<br style="line-height:25px"> else<br style="line-height:25px"> {<br style="line-height:25px"> System.out.println("usethecacheimage");<br style="line-height:25px"> cacheSuccessCount++;<br style="line-height:25px"> }<br style="line-height:25px"> allCount++;<br style="line-height:25px"> }<br style="line-height:25px"> Thread.sleep(1);<br style="line-height:25px"> }catch(InterruptedExceptione)<br style="line-height:25px"> {<br style="line-height:25px"> e.printStackTrace();<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> System.out.println("cachesize:"+cache.size());<br style="line-height:25px"> System.out.println("total:"+allCount+"successcount:"+cacheSuccessCount);<br style="line-height:25px"> System.out.println("successrate:"+cacheSuccessCount*100/allCount);<br style="line-height:25px"> }<br style="line-height:25px"> }</span><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="color:#3366ff; line-height:25px">importjava.util.LinkedHashMap;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.Map;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.Random;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.concurrent.ConcurrentLinkedQueue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicclassTest{</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:#0000ff; line-height:25px">LinkedHashMap&lt;Integer,People&gt;map=newLinkedHashMap&lt;Integer,People&gt;(16,(float)0.75,false);<br style="line-height:25px"> Peoplep1=newPeople("robin",1,28);<br style="line-height:25px"> Peoplep2=newPeople("robin",2,29);<br style="line-height:25px"> Peoplep3=newPeople("harry",3,30);<br style="line-height:25px"> map.put(newInteger(100),p1);<br style="line-height:25px"> map.put(newInteger(1),p3);<br style="line-height:25px"> map.put(newInteger(2),null);<br style="line-height:25px"> map.put(newInteger(100),p2);</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:map.values())</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">System.out.println("people:"+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:#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:#3366ff; line-height:25px">publicStringtoString()</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">publicbooleanequals(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">if(!(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">returnres;</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">publicinthashCode()</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></wbr></wbr></wbr></wbr></wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值