您好,欢迎访问上海贝乐莉网络科技有限公司!

tell
搜索

上海贝乐莉网络科技有限公司

                                          Java之LinkedHashMap

                                          文章来源:山芙 时间:2025-02-19

                                          LinkedHashMap是Map交心的哈希表战链交列表竣工,具备可先见的迭代纪律。此告竣供给全部可选的映照掌握,并愿意应用null值战null键。此类没有保护映照的序次,出格是它没有确保该按序长久没有变,LinkedHashMap竣工取HashMap的没有共的地方正在于,后者维持着1个运转于全部条款的两重链交列表。此链交列表界说了迭代程序,该迭代程序能够是拔出按次大概是拜候秩序。凭据链表中元素的顺次能够分为:按拔出次第的链表,战按拜候循序(移用get办法)的链表。默许是按拔出次序排序,假若指定按拜候依序排序,那末挪用get办法后,会将此次拜候的元素移至链表尾部,不息拜候能够构成按拜候规律排序的链表。能够誊写removeEldestEntry办法前往true值指定拔出元素时移除最老的元素。

                                          假使尔们配置那个LinkedHashMap是鉴于拜候顺次,且誊写它的removeEldestEntry,那便即是竣工了LRU算法。

                                          去望以下代码战本质后果

                                          publicstaticvoidtest1(){intinitialCapacity=10;//始初化容量floatloadFactor=0.75f;//添载果子,普通是0.75fbooleanaccessOrder=true;//排序体例false鉴于拔出递次true鉴于拜候序次Map<String,Integer>map=newLinkedHashMap<>(initialCapacity,loadFactor,accessOrder);for(inti=0;i<10;i++){map.put(String.valueOf(i),i);}//拜候前程序for(Iterator<Map.Entry<String,Integer>>it=map.entrySet().iterator();it.hasNext();){Map.Entry<String,Integer>next=it.next();System.out.println("linkedMap--before-->"+next.getKey());}System.out.println("**********************************");//模仿拜候map.get("2");//拜候后数据for(Iterator<Map.Entry<String,Integer>>it=map.entrySet().iterator();it.hasNext();){Map.Entry<String,Integer>next=it.next();System.out.println("linkedMap--after-->"+next.getKey());}}

                                          输入结果

                                          linkedMap--before-->0linkedMap--before-->1linkedMap--before-->2linkedMap--before-->3linkedMap--before-->4linkedMap--before-->5linkedMap--before-->6linkedMap--before-->7linkedMap--before-->8linkedMap--before-->9**********************************linkedMap--after-->0linkedMap--after-->1linkedMap--after-->3linkedMap--after-->4linkedMap--after-->5linkedMap--after-->6linkedMap--after-->7linkedMap--after-->8linkedMap--after-->9linkedMap--after-->2

                                          能够瞅到,当元素2被拜候后,它的序次爆发了转变。

                                          竣工LRU算法,去瞅停removeEldestEntry办法引见

                                          Thismethodtypicallydoesnotmodifythemapinanyway,insteadallowingthemaptomodifyitselfasdirectedbyitsreturnvalue.Itispermittedforthismethodtomodifythemapdirectly,butifitdoesso,itmustreturnfalse(indicatingthatthemapshouldnotattemptanyfurthermodification).Theeffectsofreturningtrueaftermodifyingthemapfromwithinthismethodareunspecified.Thisimplementationmerelyreturnsfalse(sothatthismapactslikeanormalmap-theeldestelementisneverremoved).Params:eldest–Theleastrecentlyinsertedentryinthemap,orifthisisanaccess-orderedmap,theleastrecentlyaccessedentry.Thisistheentrythatwillberemoveditthismethodreturnstrue.IfthemapwasemptypriortotheputorputAllinvocationresultinginthisinvocation,thiswillbetheentrythatwasjustinserted;inotherwords,ifthemapcontainsasingleentry,theeldestentryisalsothenewest.Returns:trueiftheeldestentryshouldberemovedfromthemap;falseifitshouldberetained.protectedbooleanremoveEldestEntry(Map.Entry<K,V>eldest){returnfalse;}

                                          默许它是没有会主动来移除的,即使曲交前往false,若是须要达成主动移除,则誊写该办法。

                                          瞧以下代码战效益

                                          publicstaticvoidtest2(){intinitialCapacity=10;//始初化容量floatloadFactor=0.75f;//添载果子,普通是0.75fbooleanaccessOrder=true;//排序体例false鉴于拔出依次true鉴于拜候次第Map<String,Integer>map=newLinkedHashMap(initialCapacity,loadFactor,accessOrder){protectedbooleanremoveEldestEntry(Map.Entryeldest){returnsize()>initialCapacity;}};for(inti=0;i<15;i++){if(i==12){//模仿拜候map.get("2");}map.put(String.valueOf(i),i);}//拜候前挨次for(Iterator<Map.Entry<String,Integer>>it=map.entrySet().iterator();it.hasNext();){Map.Entry<String,Integer>next=it.next();System.out.println("linkedMap--before-->"+next.getKey());}}

                                          输入内乱容

                                          linkedMap--before-->6linkedMap--before-->7linkedMap--before-->8linkedMap--before-->9linkedMap--before-->10linkedMap--before-->11linkedMap--before-->2linkedMap--before-->12linkedMap--before-->13linkedMap--before-->14

                                          由于那是1个定少的Map,并且尔们誊写了移除办法,移除办法内乱判定,倘若以后Map的数目年夜于设定容量了,便前往true,此时便会发作移除。

                                          正在建树值时模仿拜候了1停元素2,发掘2拜候后排正在了11前面,于是它不被移除。

                                          java.jpg

                                          推举您浏览更多相关于“ mapHashMapLinkedHashMapLRU链表 ”的作品

                                          微信

                                          扫一扫,关注我们公众号

                                          新闻中心

                                          公司名称:上海贝乐莉网络科技有限公司

                                          公司网址:上海市长宁区新乐大街34号

                                          这是描述信息

                                          扫一扫,进入手机官网

                                          Copyright © 2024-2025  上海贝乐莉网络科技有限公司   http://www.polipoliy.com  All Rights Reserved   网站地图