fork download
  1. import java.util.HashMap;
  2. import java.util.List;
  3. import java.util.stream.Collectors;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. HashMap<String, Integer> mMap = new HashMap<String, Integer>();
  8. mMap.put("CODE1", 3);
  9. mMap.put("CODE2", 5);
  10. mMap.put("CODE3", 2);
  11. mMap.put("CODE4", 4);
  12. mMap.put("CODE5", 1);
  13.  
  14. List<String> keySorted = mMap.keySet().stream()
  15. .sorted((p1, p2) -> mMap.get(p2) - mMap.get(p1))
  16. .collect(Collectors.toList());
  17.  
  18. System.out.println("表示:拡張for文");
  19. for (String key : keySorted) {
  20. System.out.printf("キー:%s 値:%d%n", key, mMap.get(key));
  21. }
  22.  
  23. System.out.println();
  24.  
  25. System.out.println("表示:forEach文");
  26. keySorted.forEach(key -> System.out.printf("キー:%s 値:%d%n", key, mMap.get(key)));
  27.  
  28. }
  29. }
  30.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty