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. import java.util.stream.Collectors ;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14.  
  15. Map< Integer , String > map = Map.of( 1 , "" , 0 , "Écrivez" , 2 , "Hello" ) ;
  16.  
  17. // Using TreeMap (Java 2+) which is a SortedMap, iterating keys in sorted order.
  18. List<String> list1 = new ArrayList<>( new TreeMap<>( map ).values() ) ;
  19.  
  20. // Using Stream (Java 8+)
  21. List<String> list2 =
  22. map
  23. .entrySet()
  24. .stream()
  25. .sorted( Map.Entry.comparingByKey() )
  26. .map( Map.Entry::getValue )
  27. .collect( Collectors.toList() )
  28. ;
  29.  
  30. System.out.println( "map.toString(): " + map ) ;
  31. System.out.println( "list1.toString(): " + list1 ) ;
  32. System.out.println( "list2.toString(): " + list2 ) ;
  33. }
  34. }
Success #stdin #stdout 0.17s 36860KB
stdin
Standard input is empty
stdout
map.toString(): {0=Écrivez, 2=Hello, 1=}
list1.toString(): [Écrivez, , Hello]
list2.toString(): [Écrivez, , Hello]