fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Map<Integer, String> map = new HashMap<Integer, String>();
  14.  
  15. map.put( 0 , "abc" );
  16. map.put( 4 , "efg" );
  17. map.put( 1 , "hij" );
  18. map.put( 3 , "klm" );
  19. map.put( 2 , "npo" );
  20.  
  21. List<Integer> keyList = new ArrayList<Integer>(map.keySet());
  22. Collections.sort(keyList);
  23. System.out.println(keyList);
  24.  
  25. map = new TreeMap<Integer, String>();
  26. map.put( 0 , "abc" );
  27. map.put( 4 , "efg" );
  28. map.put( 1 , "hij" );
  29. map.put( 3 , "klm" );
  30. map.put( 2 , "npo" );
  31. keyList = new ArrayList<Integer>(map.keySet());
  32. System.out.println(keyList);
  33. }
  34. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]