fork(1) 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.time.* ;
  8. import java.util.stream.* ;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15.  
  16. List< String > inputs = List.of( "2021-07-01", "2021-08-20", "2021-08-25", "2021-09-05", "2022-01-06", "2022-06-07" );
  17.  
  18. Map< YearMonth , Long > monthCounts =
  19. inputs
  20. .stream()
  21. .map( input -> LocalDate.parse( input ) )
  22. .collect(
  23. Collectors.groupingBy(
  24. YearMonth :: from ,
  25. Collectors.counting()
  26. )
  27. )
  28. ;
  29.  
  30. System.out.println( monthCounts ) ;
  31.  
  32. }
  33. }
Success #stdin #stdout 0.13s 50032KB
stdin
Standard input is empty
stdout
{2021-09=1, 2021-08=2, 2021-07=1, 2022-06=1, 2022-01=1}