1"use strict";(self.webpackChunkpatternize_io=self.webpackChunkpatternize_io||[]).push([[1608],{15680:(e,t,a)=>{a.d(t,{xA:()=>c,yg:()=>m});var n=a(96540);function r(e,t,a){return t in e?Object.defineProperty(e,t,{value:a,enumerable:!0,configurable:!0,writable:!0}):e[t]=a,e}function l(e,t){var a=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),a.push.apply(a,n)}return a}function o(e){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?l(Object(a),!0).forEach((function(t){r(e,t,a[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):l(Object(a)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))}))}return e}function i(e,t){if(null==e)return{};var a,n,r=function(e,t){if(null==e)return{};var a,n,r={},l=Object.keys(e);for(n=0;n<l.length;n++)a=l[n],t.indexOf(a)>=0||(r[a]=e[a]);return r}(e,t);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(n=0;n<l.length;n++)a=l[n],t.indexOf(a)>=0||Object.prototype.propertyIsEnumerable.call(e,a)&&(r[a]=e[a])}return r}var u=n.createContext({}),p=function(e){var t=n.useContext(u),a=t;return e&&(a="function"==typeof e?e(t):o(o({},t),e)),a},c=function(e){var t=p(e.components);return n.createElement(u.Provider,{value:t},e.children)},s="mdxType",y={inlineCode:"code",wrapper:function(e){var t=e.children;return n.createElement(n.Fragment,{},t)}},g=n.forwardRef((function(e,t){var a=e.components,r=e.mdxType,l=e.originalType,u=e.parentName,c=i(e,["components","mdxType","originalType","parentName"]),s=p(a),g=r,m=s["".concat(u,".").concat(g)]||s[g]||y[g]||l;return a?n.createElement(m,o(o({ref:t},c),{},{components:a})):n.createElement(m,o({ref:t},c))}));function m(e,t){var a=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var l=a.length,o=new Array(l);o[0]=g;var i={};for(var u in t)hasOwnProperty.call(t,u)&&(i[u]=t[u]);i.originalType=e,i[s]="string"==typeof e?e:r,o[1]=i;for(var p=2;p<l;p++)o[p]=a[p];return n.createElement.apply(null,o)}return n.createElement.apply(null,a)}g.displayName="MDXCreateElement"},65424:(e,t,a)=>{a.r(t),a.d(t,{assets:()=>c,contentTitle:()=>u,default:()=>m,frontMatter:()=>i,metadata:()=>p,toc:()=>s});var n=a(58168),r=a(98587),l=(a(96540),a(15680));const o=["components"],i={id:"Interface",title:"Map Interface",sidebar_label:"Interface"},u=void 0,p={unversionedId:"Language/Java/Map/Interface",id:"Language/Java/Map/Interface",title:"Map Interface",description:"Basics",source:"@site/docs/Language/Java/Map/Interface.md",sourceDirName:"Language/Java/Map",slug:"/Language/Java/Map/Interface",permalink:"/Language/Java/Map/Interface",draft:!1,tags:[],version:"current",frontMatter:{id:"Interface",title:"Map Interface",sidebar_label:"Interface"},sidebar:"CS Core",previous:{title:"HashTable",permalink:"/Language/Java/Map/HashTable"},next:{title:"LinkedHashMap",permalink:"/Language/Java/Map/LinkedHashMap"}},c={},s=[{value:"Basics",id:"basics",level:2},{value:"Put",id:"put",level:2},{value:"Remove",id:"remove",level:2},{value:"<em>Pitfall - ConcurrentModificationException</em>",id:"pitfall---concurrentmodificationexception",level:4},{value:"Iterate Through Values",id:"iterate-through-values",level:2}],y={toc:s},g="wrapper";function m(e){let t=e.components,a=(0,r.A)(e,o);return(0,l.yg)(g,(0,n.A)({},y,a,{components:t,mdxType:"MDXLayout"}),(0,l.yg)("h2",{id:"basics"},"Basics"),(0,l.yg)("ul",null,(0,l.yg)("li",{parentName:"ul"},"maps Key to Value -- ",(0,l.yg)("inlineCode",{parentName:"li"},"<K, V>")),(0,l.yg)("li",{parentName:"ul"},"no duplicated Key allowed, or it will overwrite previous value with the same Key, aka. a key can only map to at most one value"),(0,l.yg)("li",{parentName:"ul"},"deplicated Values are allowed")),(0,l.yg)("h2",{id:"put"},"Put"),(0,l.yg)("ul",null,(0,l.yg)("li",{parentName:"ul"},"Returns previous value in the Key associated:")),(0,l.yg)("pre",null,(0,l.yg)("code",{parentName:"pre"},"HashMap hm = new HashMap();\nSystem.out.println(hm.put(1, 1)); // returns Null\nSystem.out.println(hm.put(1, 2)); // 1\nSystem.out.println(hm.put(1, 3)); // 2\n")),(0,l.yg)("h2",{id:"remove"},"Remove"),(0,l.yg)("ul",null,(0,l.yg)("li",{parentName:"ul"},"HashMap remove can uses key as a parameter:")),(0,l.yg)("pre",null,(0,l.yg)("c
1ode",{parentName:"pre",className:"language-java"},"map.remove(c); // c is key\n")),(0,l.yg)("h4",{id:"pitfall---concurrentmodificationexception"},(0,l.yg)("em",{parentName:"h4"},"Pitfall - ConcurrentModificationException")),(0,l.yg)("pre",null,(0,l.yg)("code",{parentName:"pre",className:"language-java"},"for(int num : nums){\n ctr.put(num, ctr.getOrDefault(num, 0) + 1);\n if(ctr.size() > k){\n for(int key : ctr.keySet()){\n if(ctr.get(key) == 1){\n ctr.remove(key);\n } else ctr.put(key, ctr.get(key) -1);\n }\n }\n}\n")),(0,l.yg)("h2",{id:"iterate-through-values"},"Iterate Through Values"),(0,l.yg)("ul",null,(0,l.yg)("li",{parentName:"ul"},"use for loop \u2014 use ",(0,l.yg)("inlineCode",{parentName:"li"},"values()")," to iterate through values in a HashMap:")),(0,l.yg)("pre",null,(0,l.yg)("code",{parentName:"pre",className:"language-java"},"HashMap<Integer, Integer> count = new HashMap<>();\nfor (int i : count.values())\n res = Math.max(res, i);\nreturn res;\n")),(0,l.yg)("ul",null,(0,l.yg)("li",{parentName:"ul"},"use ",(0,l.yg)("inlineCode",{parentName:"li"},"Map.Entry<K, V> entry: map.entrySet()")," to iterate through both values and keys:")),(0,l.yg)("pre",null,(0,l.yg)("code",{parentName:"pre",className:"language-java"},"for(Map.Entry<Character, Integer> entry: hm.entrySet()){\n entry.getKey();\n entry.getValue();\n}\n")))}m.isMDXComponent=!0}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.