fork download
  1. import java.util.*;
  2. import java.io.*;
  3. class Main{
  4. public static void main(String[] args) throws IOException{
  5. HashMap<String,Integer> map = new HashMap<String,Integer>();
  6.  
  7. /* データを読み込んで、mapにドンドン追加 */
  8. while(true){
  9. String name = br.readLine();
  10. int score = 0;
  11. /* nameがnullだったときは、equalsメソッドも使えないね */
  12. if(name == null){
  13. break;
  14. }
  15. score = Integer.parseInt(br.readLine());
  16. map.put(name,score);
  17. }
  18. ArrayList<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>();
  19. list.addAll((new HashMap<String,Integer>(map)).entrySet());
  20.  
  21. Collections.sort(list,Collections.reverseOrder(new SampleSorter()));
  22.  
  23. for(Map.Entry<String,Integer> m:list){
  24. System.out.println(m.getKey());
  25. System.out.println(m.getValue());
  26. }
  27.  
  28. }
  29. }
  30.  
  31. class SampleSorter implements Comparator<Map.Entry<String,Integer>>{
  32. public int compare(Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2){
  33. return o1.getValue() - o2.getValue();
  34. }
  35. public boolean equals(Object o){
  36. return this == o;
  37. }
  38. }
Success #stdin #stdout 0.04s 213312KB
stdin
相沢
37
石田
48
臼井
57
遠藤
36
小川
82
加藤
55
木島
37
工藤
25
stdout
小川
82
臼井
57
加藤
55
石田
48
木島
37
相沢
37
遠藤
36
工藤
25