on
■Map을 Key값이 아닌 Value를 이용하여 정렬
■Map을 Key값이 아닌 Value를 이용하여 정렬
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { private static BufferedReader br = new BufferedReader( new InputStreamReader( System . in )); private static BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( System . out )); static class Point{ int idx; int total; public Point( int idx, int total) { this .idx = idx; this .total = total; } } public static void main( String [] args) throws IOException{ Map < String , Point > map = new HashMap < > (); map.put( "Nepal" , new Point( 1 , 100 )); map.put( "United States" , new Point( 2 , 400 )); map.put( "India" , new Point( 3 , 50 )); map.put( "England" , new Point( 4 , 1000 )); map.put( "Australia" , new Point( 5 , 300 )); List < Point > valueList = new ArrayList < > (map.values()); valueList.sort((p1,p2) - > p2.total - p1.total); for (Point point : valueList) { System . out . println (point.total + " " + point.idx); } bw.write( "" ); bw.flush(); bw.close(); } // private static int stoi(String input) { // return Integer.parseInt(input); // } } Colored by Color Scripter
from http://aig2029.tistory.com/328 by ccl(A) rewrite - 2021-10-08 18:01:43