{"id":2873,"date":"2019-06-10T23:56:51","date_gmt":"2019-06-10T14:56:51","guid":{"rendered":"https:\/\/now0930.pe.kr\/wordpress\/?p=2873"},"modified":"2019-06-11T00:06:38","modified_gmt":"2019-06-10T15:06:38","slug":"%ed%94%84%eb%a1%9c%ea%b7%b8%eb%9e%98%eb%a8%b8%ec%8a%a4-%eb%b2%a0%ec%8a%a4%ed%8a%b8%ec%95%a8%eb%b2%94","status":"publish","type":"post","link":"https:\/\/now0930.pe.kr\/wordpress\/%ed%94%84%eb%a1%9c%ea%b7%b8%eb%9e%98%eb%a8%b8%ec%8a%a4-%eb%b2%a0%ec%8a%a4%ed%8a%b8%ec%95%a8%eb%b2%94\/","title":{"rendered":"\ud504\ub85c\uadf8\ub798\uba38\uc2a4 \ubca0\uc2a4\ud2b8\uc568\ubc94"},"content":{"rendered":"\n<p>\ub0b4\uac00 \ub108\ubb34 \uc5b4\ub835\uac8c \ud478\ub098? \ub2e4\ub978 \uc0ac\ub78c \ucf54\ub4dc\ub294 \uc27d\ub358\ub370. \uc774 \ubb38\uc81c\uac00 java Collection framework\ub97c \uc544\ub294\uc9c0 \ubb3c\uc5b4\ubcf8\ub2e4. \ubb3c\ub860 \ub09c \uc798 \ubaa8\ub974\uc9c0\ub9cc.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport java.util.ArrayList;\nimport java.util.Comparator;\nimport java.util.Iterator;\nimport java.util.TreeMap;\n\npublic class Main {\n\n\tpublic static void main(String[] args) {\n\t\t\/\/ TODO Auto-generated method stub\n\t\t\/\/ \uc2dc\uc791..\n\t\t\/\/\t\tString[] operations = { \"I16\", \"I10\", \"I20\", \"D1\" };\n\n\t\tString[] genres = { \"classic\", \"pop\", \"classic\", \"classic\", \"pop\" };\n\t\tint[] plays = new int[] { 500, 600, 150, 800, 2500 };\n\t\tSolution sol = new Solution();\n\t\tint[] answer = new int[10];\n\t\tanswer = sol.solution(genres, plays);\n\t\tSystem.out.println(\"\ub2f5\uc740\"+answer);\n\n\t}\n}\n\nclass Solution {\n\tpublic int[] solution(String[] genres, int[] plays) {\n\t\tint[] answer;\n\t\tTreeMap&lt;String, Integer> genresSelcetion = new TreeMap&lt;>();\n\t\tTreeMap&lt;Integer, Integer> genresIndex = new TreeMap&lt;>();\n\t\tArrayList&lt;Integer> bestAlbum = new ArrayList&lt;>();\n\n\t\tfor (int i = 0; i &lt; genres.length; i++) {\n\t\t\tint count = 0;\n\t\t\t\/\/\t\t\tcount = genresSelcetion.get(genres[i]);\n\t\t\tif (genresSelcetion.get(genres[i]) == null) {\n\t\t\t\t\/\/genres\uac00 \uc5c6\uc74c.\n\t\t\t\tgenresSelcetion.put(genres[i], plays[i]);\n\t\t\t\t\/\/\t\t\t\tSystem.out.println(\"String\uc740\" + genres[i]);\n\t\t\t} else { \/\/genres\uac00 \uc788\uc74c.\n\t\t\t\tcount = genresSelcetion.get(genres[i]);\n\t\t\t\tgenresSelcetion.put(genres[i], count + plays[i]);\n\n\t\t\t}\n\t\t\tSystem.out.println(\"\uce74\uc6b4\ud2b8\" + count);\n\t\t}\n\n\t\t\/\/TreeMap \uc67c\ucabd \uc815\ub82c.\n\t\tComparator&lt;String> comparator = new ValueComparator&lt;String, Integer>(genresSelcetion);\n\t\tTreeMap&lt;String, Integer> sortedGenresSelection = new TreeMap&lt;>(comparator);\n\t\tsortedGenresSelection.putAll(genresSelcetion);\n\t\tIterator&lt;String> itrGenres = sortedGenresSelection.keySet().iterator();\n\n\t\t\/\/\uc785\ub825\ub41c String[] genres - key, \uace1 index - value\ub85c \ub9cc\ub4e6.\n\t\twhile (itrGenres.hasNext()) {\n\t\t\tString key = itrGenres.next();\n\t\t\tSystem.out.println(\"Key=\" + key);\n\t\t\tfor (int i = 0; i &lt; genres.length; i++) {\n\t\t\t\tif (genres[i].equals(key)) {\n\t\t\t\t\tgenresIndex.put(i, plays[i]);\n\t\t\t\t\tSystem.out.println(\"No\" + i + \" is genres=\" + genres[i] + \", plays =\" + plays[i]);\n\t\t\t\t\t\/\/\ub123\uc744\ub54c \uc815\ub82c.\n\t\t\t\t}\n\n\t\t\t\tSystem.out.println(\"\uc804\uccb4 value\uc740\" + genresIndex.values());\n\t\t\t} \/\/for\n\t\t\t\t\/\/\ud55c \uc7a5\ub974 \ubaa8\ub450 \ub05d\ub09c \ud6c4, \uac12\uc73c\ub85c \uc815\ub82c\n\t\t\t\t\/\/\ub2e4\uc2dc \uc0ac\uc6a9\ud558\uae30 \uc704\ud574 loop \uc548\uc5d0\uc11c \uc120\uc5b8\n\t\t\tComparator&lt;Integer> comparator2 = new ValueComparator&lt;Integer, Integer>(genresIndex){\n\n\t\t\t\t@Override\n\t\t\t\tpublic int compare(Integer arg0, Integer arg1) {\n\t\t\t\t\t\/\/ TODO Auto-generated method stub\n\t\t\t\t\treturn map.get(arg0)> map.get(arg1)?-1:1;\n\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t\n\t\t\t};\n\t\t\tTreeMap&lt;Integer, Integer> sortedGenresIndex = new TreeMap&lt;>(comparator2);\n\t\t\tsortedGenresIndex.putAll(genresIndex);\n\t\t\tIterator&lt;Integer> itrGenresIndex = sortedGenresIndex.keySet().iterator();\n\t\t\tint j = 0;\n\t\t\twhile (itrGenresIndex.hasNext()) {\n\t\t\t\t\/\/\uc815\ub9ac\ud55c \uc21c\uc11c\ub97c Array List\uc5d0 \uc0bd\uc785\n\t\t\t\tInteger playIndex = itrGenresIndex.next();\n\t\t\t\tj++;\n\t\t\t\tif (j > 2)\n\t\t\t\t\tbreak;\n\t\t\t\tbestAlbum.add(playIndex);\n\t\t\t}\n\n\t\t\tSystem.out.println(\"\uc5ec\uae30 \ud655\uc778!! value\uc740\" + sortedGenresIndex.values());\n\t\t\tfor (int k = 0; k &lt; bestAlbum.size(); k++)\n\t\t\t\tSystem.out.println(\"\uc5ec\uae30\ub294 ArrayList\" + bestAlbum.get(k));\n\n\t\t\t\/\/clear TreeMap..\n\t\t\tsortedGenresIndex.clear();\n\t\t\tgenresIndex.clear();\n\n\t\t} \/\/ while\n\t\t\/*\n\t\t * while (itrGenres.hasNext()) {\n\t\t * \n\t\t * Object key = itrGenres.next(); System.out.println(\"key\ub294\" + key);\n\t\t * System.out.println(\"value\ub294\" + sortedGenresSelection.get(key));\n\t\t * \n\t\t * }\n\t\t * \n\t\t *\/\n\t\t\/\/\t\tSystem.out.println(\"\uc804\uccb4 value\uc740\" + sortedGenresSelection.values());\n\n\t\tanswer = new int[bestAlbum.size()];\n\t\tfor (int l = 0; l &lt; bestAlbum.size(); l++) {\n\t\t\tanswer[l] = bestAlbum.get(l);\n\n\t\t}\n\t\treturn answer;\n\n\t}\n\n}\/\/Solution\n\nclass ValueComparator&lt;K, V extends Comparable&lt;V>> implements Comparator&lt;K> {\n\tTreeMap&lt;K, V> map = new TreeMap&lt;K, V>();\n\n\tpublic ValueComparator(TreeMap&lt;K, V> map) {\n\t\tthis.map.putAll(map);\n\t}\n\n\t@Override\n\tpublic int compare(K arg0, K arg1) {\n\t\t\/\/ TODO Auto-generated method stub\n\t\treturn -map.get(arg0).compareTo(map.get(arg1));\n\t}\n\n} \/\/class ValueComparator<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/www.programcreek.com\/2013\/03\/java-sort-map-by-value\/\">TreeMap Value\ub85c \uc815\ub82c<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/starblood.tistory.com\/m\/entry\/Map-HashMap-%EC%88%9C%ED%9A%8C%ED%95%98%EA%B8%B0\">keyset, entryset \ucc28\uc774<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/109383\/sort-a-mapkey-value-by-values\">java8 \uc774\ud6c4\ub85c \ucd94\uac00\ub41c treemap \uc815\ub82c<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ub0b4\uac00 \ub108\ubb34 \uc5b4\ub835\uac8c \ud478\ub098? \ub2e4\ub978 \uc0ac\ub78c \ucf54\ub4dc\ub294 \uc27d\ub358\ub370. \uc774 \ubb38\uc81c\uac00 java Collection framework\ub97c \uc544\ub294\uc9c0 \ubb3c\uc5b4\ubcf8\ub2e4. \ubb3c\ub860 \ub09c \uc798 \ubaa8\ub974\uc9c0\ub9cc. TreeMap [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[12],"tags":[98,584,586,585],"class_list":["post-2873","post","type-post","status-publish","format-standard","hentry","category-12","tag-java","tag-programmers","tag-586","tag-585"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/2873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/comments?post=2873"}],"version-history":[{"count":6,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/2873\/revisions"}],"predecessor-version":[{"id":2881,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/2873\/revisions\/2881"}],"wp:attachment":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/media?parent=2873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/categories?post=2873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/tags?post=2873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}