ÃֽŠ°Ô½Ã±Û(JAVA)
2017.07.05 / 02:24
[Java] HashMap Value Á¤·Ä
Ŭ·¡½Ä·Î¾â
Ãßõ ¼ö 256
[Java] HashMap Value Á¤·Ä
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class Test { public static void main(String[] args) { Map<string,integer> map = new HashMap<string,integer>(); map.put( "a" , 3 ); map.put( "b" , 2 ); map.put( "c" , 1 ); System.out.println( "------------sort Àü -------------" ); System.out.println(map); Iterator it = Test.sortByValue(map).iterator(); System.out.println( "---------sort ÈÄ------------" ); while (it.hasNext()){ String temp = (String) it.next(); System.out.println(temp + " = " + map.get(temp)); } //System.out.println(map); } public static List sortByValue( final Map map){ List<string> list = new ArrayList(); list.addAll(map.keySet()); Collections.sort(list, new Comparator(){ public int compare(Object o1,Object o2){ Object v1 = map.get(o1); Object v2 = map.get(o2); return ((Comparable) v1).compareTo(v2); } }); Collections.reverse(list); // ÁÖ¼®½Ã ¿À¸§Â÷¼ø return list; } } </string></string,integer></string,integer> |
Ãâó: http://ekfqkqhd.tistory.com/entry/Java-HashMap-Value-Á¤·Ä [½Ã¿ì¾Æºü´ÔÀÇ ºí·Î±×]