HashMap(1.2)
<wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px">HashMap</span><span style="color:#000080; line-height:25px">是基于哈希表的Map接口的实现。此实现提供所有可选的映射操作,并允许使用null值和null键。</span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#003366; line-height:25px">(除了不同步和允许使用null之外,HashMap类与Hashtable大致相同。)此类不保证映射的顺序,特别是它不保证该顺序恒久不变。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">此实现假定哈希函数将元素正确分布在各桶之间,可为基本操作(get和put)提供稳定的性能。</span><br style="line-height:25px"><span style="color:#000080; line-height:25px"><wbr style="line-height:25px">迭代集合视图所需的时间与HashMap实例的“</wbr></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">(键-值映射关系数)的和成比例<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 所以,如果迭代性能很重要,则不要将初始容量设置得太高(或将加载因子设置得太低)。<br style="line-height:25px"><span style="color:#003366; line-height:25px">HashMap的实例有两个参数影响其性能:</span><span style="color:#99cc00; line-height:25px">初始容量</span><span style="color:#003366; line-height:25px">和</span><span style="color:#808000; line-height:25px">加载因子</span><span style="color:#003366; line-height:25px">。</span><span style="color:#993300; line-height:25px">容量</span><span style="color:#000080; line-height:25px">是哈希表中桶的数量,</span><span style="color:#993300; line-height:25px">初始容量</span><span style="color:#000080; line-height:25px">只是哈希表在创建时的容量。<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">加载因子</span><span style="color:#000080; line-height:25px">是哈希表在其容量自动增加之前可以达到多满的一种尺度。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">当哈希表中的条目数超出了加载因子与当前容量的乘积时,通过调用rehash方法将容量翻倍。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">通常,默认加载因子(.75)在时间和空间成本上寻求一种折衷。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">加载因子过高虽然减少了空间开销,但同时也增加了查询成本</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">(在大多数HashMap类的操作中,包括get和put操作,都反映了这一点)。</span><br style="line-height:25px"> 在设置初始容量时应该考虑到映射中所需的条目数及其加载因子,以便最大限度地降低rehash操作次数。<br style="line-height:25px"> 如果最大条目数小于初始容量乘以加载因子,则不会发生rehash操作。<br style="line-height:25px"> 如果很多映射关系要存储在HashMap实例中,则相对于按需执行自动的rehash操作以增大表的容量来说,<br style="line-height:25px"> 使用足够大的初始容量创建它将使得映射关系能更有效地存储。<br style="line-height:25px"> 注意,此实现不是同步的。如果多个线程同时访问此映射,而其中至少一个线程从结构上修改了该映射,则它必须保持外部同步。<br style="line-height:25px"> (结构上的修改是指添加或删除一个或多个映射关系的操作;仅改变与实例已经包含的键关联的值不是结构上的修改。)<br style="line-height:25px"> 这一般通过对自然封装该映射的对象进行同步操作来完成。如果不存在这样的对象,<br style="line-height:25px"> 则应该使用Collections.synchronizedMap方法来“包装”该映射。最好在创建时完成这一操作,以防止对映射进行意外的不同步访问,如下所示:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Mapm=Collections.synchronizedMap(newHashMap(...));</span><br style="line-height:25px"> 由所有此类的“集合视图方法”所返回的迭代器都是快速失败的:在迭代器创建之后,<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="line-height:25px">注意1</span>:允许使用null值和null键。<br style="line-height:25px"><span style="line-height:25px">注意2</span>:此实现不是同步的。不是线程安全的。<br style="line-height:25px"><span style="line-height:25px">注意3</span>:这里的键也是个对象(本质上是使用它的hashCode())。<br style="line-height:25px"><span style="line-height:25px">注意4</span>:如果插入重复的元素,后面的会覆盖前面的。如例1所示。<br style="line-height:25px"><span style="line-height:25px">注意5</span>:除了不同步和允许使用null之外,HashMap类与Hashtable大致相同,关于更多的内容可以查考Hashtable<br style="line-height:25px"><span style="line-height:25px">实例1</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.Comparator;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.EnumSet;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.HashMap;</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">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:#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">HashMap<Integer,People>map=newHashMap();<br style="line-height:25px"> Peoplep1=newPeople("robin",1,28);<br style="line-height:25px"> Peoplep2=newPeople("robin",2,30);<br style="line-height:25px"> map.put(newInteger(100),p1);<br style="line-height:25px"> map.put(newInteger(100),p2);<br style="line-height:25px"> for(Peoplep:map.values())<br style="line-height:25px"> {<br style="line-height:25px"> System.out.println(p);<br style="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"></span><span style="color:#ff6600; line-height:25px">People</span><span style="color:#3366ff; line-height:25px">{</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"></span><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"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">id+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">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"></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"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">name.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 style="line-height:25px"><span style="color:#ff00ff; line-height:25px">HashMap</span><span style="color:#000080; line-height:25px">是基于哈希表的Map接口的实现。此实现提供所有可选的映射操作,并允许使用null值和null键。</span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#003366; line-height:25px">(除了不同步和允许使用null之外,HashMap类与Hashtable大致相同。)此类不保证映射的顺序,特别是它不保证该顺序恒久不变。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">此实现假定哈希函数将元素正确分布在各桶之间,可为基本操作(get和put)提供稳定的性能。</span><br style="line-height:25px"><span style="color:#000080; line-height:25px"><wbr style="line-height:25px">迭代集合视图所需的时间与HashMap实例的“</wbr></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">(键-值映射关系数)的和成比例<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 所以,如果迭代性能很重要,则不要将初始容量设置得太高(或将加载因子设置得太低)。<br style="line-height:25px"><span style="color:#003366; line-height:25px">HashMap的实例有两个参数影响其性能:</span><span style="color:#99cc00; line-height:25px">初始容量</span><span style="color:#003366; line-height:25px">和</span><span style="color:#808000; line-height:25px">加载因子</span><span style="color:#003366; line-height:25px">。</span><span style="color:#993300; line-height:25px">容量</span><span style="color:#000080; line-height:25px">是哈希表中桶的数量,</span><span style="color:#993300; line-height:25px">初始容量</span><span style="color:#000080; line-height:25px">只是哈希表在创建时的容量。<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">加载因子</span><span style="color:#000080; line-height:25px">是哈希表在其容量自动增加之前可以达到多满的一种尺度。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">当哈希表中的条目数超出了加载因子与当前容量的乘积时,通过调用rehash方法将容量翻倍。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">通常,默认加载因子(.75)在时间和空间成本上寻求一种折衷。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">加载因子过高虽然减少了空间开销,但同时也增加了查询成本</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">(在大多数HashMap类的操作中,包括get和put操作,都反映了这一点)。</span><br style="line-height:25px"> 在设置初始容量时应该考虑到映射中所需的条目数及其加载因子,以便最大限度地降低rehash操作次数。<br style="line-height:25px"> 如果最大条目数小于初始容量乘以加载因子,则不会发生rehash操作。<br style="line-height:25px"> 如果很多映射关系要存储在HashMap实例中,则相对于按需执行自动的rehash操作以增大表的容量来说,<br style="line-height:25px"> 使用足够大的初始容量创建它将使得映射关系能更有效地存储。<br style="line-height:25px"> 注意,此实现不是同步的。如果多个线程同时访问此映射,而其中至少一个线程从结构上修改了该映射,则它必须保持外部同步。<br style="line-height:25px"> (结构上的修改是指添加或删除一个或多个映射关系的操作;仅改变与实例已经包含的键关联的值不是结构上的修改。)<br style="line-height:25px"> 这一般通过对自然封装该映射的对象进行同步操作来完成。如果不存在这样的对象,<br style="line-height:25px"> 则应该使用Collections.synchronizedMap方法来“包装”该映射。最好在创建时完成这一操作,以防止对映射进行意外的不同步访问,如下所示:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Mapm=Collections.synchronizedMap(newHashMap(...));</span><br style="line-height:25px"> 由所有此类的“集合视图方法”所返回的迭代器都是快速失败的:在迭代器创建之后,<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="line-height:25px">注意1</span>:允许使用null值和null键。<br style="line-height:25px"><span style="line-height:25px">注意2</span>:此实现不是同步的。不是线程安全的。<br style="line-height:25px"><span style="line-height:25px">注意3</span>:这里的键也是个对象(本质上是使用它的hashCode())。<br style="line-height:25px"><span style="line-height:25px">注意4</span>:如果插入重复的元素,后面的会覆盖前面的。如例1所示。<br style="line-height:25px"><span style="line-height:25px">注意5</span>:除了不同步和允许使用null之外,HashMap类与Hashtable大致相同,关于更多的内容可以查考Hashtable<br style="line-height:25px"><span style="line-height:25px">实例1</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.Comparator;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.EnumSet;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.HashMap;</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">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:#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">HashMap<Integer,People>map=newHashMap();<br style="line-height:25px"> Peoplep1=newPeople("robin",1,28);<br style="line-height:25px"> Peoplep2=newPeople("robin",2,30);<br style="line-height:25px"> map.put(newInteger(100),p1);<br style="line-height:25px"> map.put(newInteger(100),p2);<br style="line-height:25px"> for(Peoplep:map.values())<br style="line-height:25px"> {<br style="line-height:25px"> System.out.println(p);<br style="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"></span><span style="color:#ff6600; line-height:25px">People</span><span style="color:#3366ff; line-height:25px">{</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"></span><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"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">id+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">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"></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"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">name.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>