HashSet 与HashMap底层实现

<h1>
    <span class="link_title"><a href="/rengq126/article/details/7686377">
    HashSet 与HashMap底层实现            
    </a></span>
</h1>
    <div class="article_manage clearfix">
    <div class="article_l">
        <span class="link_categories">
        标签:
          <a href="http://www.csdn.net/tag/hashmap" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">hashmap</a>
        </span>
    </div>
    <div class="article_r">
        <span class="link_postdate">2012-06-23 17:28</span>
        <span class="link_view" title="阅读次数">5410人阅读</span>
        <span class="link_comments" title="评论次数"> <a href="#comments" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(0)</span>
        <span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" onclick="javascript:collectArticle('HashSet+%e4%b8%8eHashMap%e5%ba%95%e5%b1%82%e5%ae%9e%e7%8e%b0','7686377');return false;" title="收藏" target="_blank">收藏</a></span>
         <span class="link_report"> <a href="#report" onclick="javascript:report(7686377,2);return false;" title="举报">举报</a></span>

    </div>
</div>
<div class="embody" style="display:none" id="embody">
    <span class="embody_t">本文章已收录于:</span>
    <div class="embody_c" id="lib" value="{&quot;err&quot;:0,&quot;msg&quot;:&quot;ok&quot;,&quot;data&quot;:[]}"></div>
</div>
<style type="text/css">        
        .embody{
            padding:10px 10px 10px;
            margin:0 -20px;
            border-bottom:solid 1px #ededed;                
        }
        .embody_b{
            margin:0 ;
            padding:10px 0;
        }
        .embody .embody_t,.embody .embody_c{
            display: inline-block;
            margin-right:10px;
        }
        .embody_t{
            font-size: 12px;
            color:#999;
        }
        .embody_c{
            font-size: 12px;
        }
        .embody_c img,.embody_c em{
            display: inline-block;
            vertical-align: middle;               
        }
         .embody_c img{               
            width:30px;
            height:30px;
        }
        .embody_c em{
            margin: 0 20px 0 10px;
            color:#333;
            font-style: normal;
        }
</style>
<script type="text/javascript">
    $(function () {
        try
        {
            var lib = eval("("+$("#lib").attr("value")+")");
            var html = "";
            if (lib.err == 0) {
                $.each(lib.data, function (i) {
                    var obj = lib.data[i];
                    //html += '<img src="' + obj.logo + '"/>' + obj.name + "&nbsp;&nbsp;";
                    html += ' <a href="' + obj.url + '" target="_blank">';
                    html += ' <img src="' + obj.logo + '">';
                    html += ' <em><b>' + obj.name + '</b></em>';
                    html += ' </a>';
                });
                if (html != "") {
                    setTimeout(function () {
                        $("#lib").html(html);                      
                        $("#embody").show();
                    }, 100);
                }
            }      
        } catch (err)
        { }
        
    });
</script>
  <div class="category clearfix">
    <div class="category_l">
       <img src="http://static.blog.csdn.net/images/category_icon.jpg">
        <span>分类:</span>
    </div>
    <div class="category_r">
                <label onclick="GetCategoryArticles('1172553','rengq126','top','7686377');">
                    <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">学习笔记<em>(7)</em></span>
                  <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">
                  <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">
                    <div class="subItem">
                        <div class="subItem_t"><a href="http://blog.csdn.net/rengq126/article/category/1172553" target="_blank">作者同类文章</a><i class="J_close">X</i></div>
                        <ul class="subItem_l" id="top_1172553">                            
                        </ul>
                    </div>
                </label>                    
    </div>
</div>
<script type="text/javascript" src="http://static.blog.csdn.net/scripts/category.js"></script>  
    <div class="bog_copyright">         
        <p class="copyright_p">版权声明:本文为博主原创文章,未经博主允许不得转载。</p>
    </div>

1. HashSet底层通过包装HashMap来实现,HashSet在添加一个值的时候,实际上是将此值作为HashMap中的key来进行保存。

2. HashMap的底层实现是通过初始化化一个Entry数组来实现key、value的保存。

3. 在HashMap的Entry中有四个变量,key、value、hash、next,其中next用于在hash方法添加值冲突时候,所指向的下一个值。

4. 在HashMap中添加值步骤

    第一步,对key的hashcode进行hash计算,获取应该保存到数组中的index。

   第二步,判断index所指向的数组元素是否为空,如果为空则直接插入。

   第三步,如果不为空,则依次查找entry中next所指定的元素,判读key是否相等,如果相等,则替换久的值,返回。

   第四步,如果都不相等,则将此链表头元素赋值给待插入entry的next变量,让后将待插入元素插入到entry数组中去。

5. map遍历方法有两种,keyset 和Map.Entry

    <div id="digg" articleid="7686377">
        <dl id="btnDigg" class="digg digg_disable" onclick="btndigga();">
           
             <dt>顶</dt>
            <dd>2</dd>
        </dl>
       
          
        <dl id="btnBury" class="digg digg_disable" onclick="btnburya();">
          
              <dt>踩</dt>
            <dd>1</dd>               
        </dl>
        
    </div>
 <div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);" target="_blank">&nbsp;</a>   </div>
<div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);" target="_blank">&nbsp;</a></div>
<script type="text/javascript">
            function btndigga() {
                $(".tracking-ad[data-mod='popu_222'] a").click();
            }
            function btnburya() {
                $(".tracking-ad[data-mod='popu_223'] a").click();
            }
        </script>
<div style="clear:both; height:10px;"></div>


    <div class="similar_article" style="">
            <h4>我的同类文章</h4>
            <div class="similar_c" style="margin:20px 0px 0px 0px">
                <div class="similar_c_t">
                            <label class="similar_cur">
                                <span style="cursor:pointer" onclick="GetCategoryArticles('1172553','rengq126','foot','7686377');">学习笔记<em>(7)</em></span>
                            </label>
                </div>
               
                <div class="similar_wrap tracking-ad" data-mod="popu_141" style="max-height:195px;">
                    <a href="http://blog.csdn.net" style="display:none" target="_blank">http://blog.csdn.net</a>
                    <ul class="similar_list fl"><li><em>•</em><a href="http://blog.csdn.net/rengq126/article/details/51095333" id="foot_aritcle_51095333undefined9876512687230425" target="_blank" title="nutch使用入门">nutch使用入门</a><span>2016-04-08</span><label><i>阅读</i><b>270</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/rengq126/article/details/49463981" id="foot_aritcle_49463981undefined12577701206868674" target="_blank" title="单点登录">单点登录</a><span>2015-10-28</span><label><i>阅读</i><b>452</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/rengq126/article/details/7772663" id="foot_aritcle_7772663undefined6711990817726057" target="_blank" title="servlet 学习">servlet 学习</a><span>2012-07-22</span><label><i>阅读</i><b>204</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/rengq126/article/details/7686639" id="foot_aritcle_7686639undefined42354680316263127" target="_blank" title="策略模式学习">策略模式学习</a><span>2012-06-23</span><label><i>阅读</i><b>378</b></label></li> </ul>

                    <ul class="similar_list fr"><li><em>•</em><a href="http://blog.csdn.net/rengq126/article/details/49700351" id="foot_aritcle_49700351undefined8386517457918314" target="_blank" title="软件架构师职责">软件架构师职责</a><span>2015-11-07</span><label><i>阅读</i><b>446</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/rengq126/article/details/7964929" id="foot_aritcle_7964929undefined7103424462276136" target="_blank" title="Spring代码分析一:加载与初始化">Spring代码分析一:加载与初始化</a><span>2012-09-10</span><label><i>阅读</i><b>7183</b></label></li> <li><em>•</em><a href="http://blog.csdn.net/rengq126/article/details/7748794" id="foot_aritcle_7748794undefined25842622943567006" target="_blank" title="OSGI">OSGI</a><span>2012-07-15</span><label><i>阅读</i><b>230</b></label></li> </ul>
                </div>
            </div>
        </div>    
<script type="text/javascript">
    $(function () {
        GetCategoryArticles('1172553', 'rengq126','foot','7686377');
    });
</script>
<div>
        <div class="J_adv" data-view="true" data-mod="ad_popu_205" data-mtp="43" data-order="114" data-con="ad_content_1900" style="width: 728px; height: 90px;"><script src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-8990951720398508" data-ad-slot="8267689356/3776917242" data-adsbygoogle-status="done"><ins id="aswift_0_expand" style="display:inline-table;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px;background-color:transparent"><ins id="aswift_0_anchor" style="display:block;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px;background-color:transparent"><iframe width="728" height="90" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_0" name="aswift_0" style="left:0;position:absolute;top:0;"></iframe></ins></ins></ins><script>(adsbygoogle=window.adsbygoogle || []).push({});</script></div> 
 </div>
 <dt><span>猜你在找</span></dt>    





<div id="adCollege" style="width: 42%;float: left;"> 
    <script src="http://csdnimg.cn/jobreco/job_reco.js" type="text/javascript"></script> 
    <script type="text/javascript">
        csdn.position.showEdu({
            sourceType: "blog",
            searchType: "detail",
            searchKey: "7686377",
            username: "",
            recordcount: "5",
            containerId: "adCollege" //容器DIV的id。 
        });
    </script> 
<div class="tracking-ad" data-mod="popu_84"><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/1512" title="数据结构基础系列(5):数组与广义表" strategy="v4:content" target="_blank">数据结构基础系列(5):数组与广义表</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/1428" title="4.5.数组&amp;字符串&amp;结构体&amp;共用体&amp;枚举-C语言高级专题第5部分" strategy="v4:content" target="_blank">4.5.数组&amp;字符串&amp;结构体&amp;共用体&amp;枚举-C语言高级专题第5部分</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/1120" title="C语言系列之 数组与算法实战" strategy="v4:content" target="_blank">C语言系列之 数组与算法实战</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/1139" title="C语言系列之 数组强化与三级指针" strategy="v4:content" target="_blank">C语言系列之 数组强化与三级指针</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px; white-space: nowrap;"><a href="http://edu.csdn.net/course/detail/1131" title="C语言系列之 指针与数组总复习" strategy="v4:content" target="_blank">C语言系列之 指针与数组总复习</a></dd></div></div>  


 <div id="res" data-mod="popu_36" class="tracking-ad" style="width: 42%; float: left; margin-right: 30px; display: block;"><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/managermeng/article/details/41116847" title="Java中HashMap和HashSet存储机制" strategy="SearchAlgorithm" target="_blank">Java中HashMap和HashSet存储机制</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/u010156024/article/details/48374865" title="java-HashMap和HashSet源码分析" strategy="SearchAlgorithm" target="_blank">java-HashMap和HashSet源码分析</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/u010282707/article/details/17614739" title="通过分析 JDK 源代码研究 TreeMap 红黑树算法实现" strategy="SearchAlgorithm" target="_blank">通过分析 JDK 源代码研究 TreeMap 红黑树算法实现</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/xiewenbo/article/details/12379969" title="探索 ConcurrentHashMap 高并发性的实现机制" strategy="SearchAlgorithm" target="_blank">探索 ConcurrentHashMap 高并发性的实现机制</a></dd><dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="http://blog.csdn.net/u010255859/article/details/51247362" title="深入源码分析HashSet" strategy="SearchAlgorithm" target="_blank">深入源码分析HashSet</a></dd></div>
<div id="ad_cen">        
<!-- 广告位开始 -->
<div class="J_adv" data-view="true" data-mod="ad_popu_72" data-mtp="61" data-order="114" data-con="ad_content_1937"><script id="popuLayer_js_q" src="http://ads.csdn.net/js/popuLayer.js" defer="" type="text/javascript"></script><div id="location_parent_google"></div><script src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><div id="layerd_google" style="position: fixed; bottom: 0px; right: 0px; line-height: 0px; z-index: 1000; width: 302px; height: 275px; display: none;"><div class="J_close layer_close" style="display:;background-color:#efefef;padding:0px;color:#333;font:12px/24px Helvetica,Tahoma,Arial,sans-serif;text-align:right;">关闭</div><ins class="adsbygoogle" style="display: inline-block; width: 300px; height: 250px;" data-ad-client="ca-pub-8990951720398508" data-ad-slot="8267689356/2658895482" data-adsbygoogle-status="done"><ins id="aswift_2_expand" style="display:inline-table;border:none;height:250px;margin:0;padding:0;position:relative;visibility:visible;width:300px;background-color:transparent"><ins id="aswift_2_anchor" style="display:block;border:none;height:250px;margin:0;padding:0;position:relative;visibility:visible;width:300px;background-color:transparent"><iframe width="300" height="250" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" id="aswift_2" name="aswift_2" style="left:0;position:absolute;top:0;"></iframe></ins></ins></ins></div><script>(adsbygoogle=window.adsbygoogle || []).push({});</script><script>  document.getElementById('popuLayer_js_q').onload=function(){      var styObjd=styObj={width:'302px',height:'275px'};window.CSDN.Layer.PopuLayer('#layerd_google',{storageName:'layerd',styleObj:styObjd,total:50,expoire:1000*60});  }</script></div>
<!-- 广告位结束 -->
查看评论

  暂无评论

* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
<div id="ad_bot">
</div>
    <a id="quick-reply" class="btn btn-top q-reply" title="快速回复" style="display:none;">
        <img src="http://static.blog.csdn.net/images/blog-icon-reply.png" alt="快速回复">
    </a>    
<a id="d-top-a" class="btn btn-top backtop" style="display: none;" title="返回顶部" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_huidaodingbu'])">         
     <img src="http://static.blog.csdn.net/images/top.png" alt="TOP">
</a>

                    <div class="clear">
                    </div>
                </div>
posted @ 2017-01-16 10:33  jobs-lgy  阅读(198)  评论(0编辑  收藏  举报